{"id":692,"library":"pyzmq","title":"PyZMQ - Python bindings for ZeroMQ","description":"PyZMQ is the Python binding for the ZeroMQ (ØMQ) messaging library. ZeroMQ provides a high-performance, asynchronous messaging library, and PyZMQ allows Python applications to leverage its various messaging patterns (PUB/SUB, REQ/REP, PUSH/PULL, PAIR) for building distributed systems. The library is actively maintained with frequent minor releases, currently at version 27.1.0, and ships with bundled `libzmq` for easy installation.","status":"active","version":"27.1.0","language":"python","source_language":"en","source_url":"https://github.com/zeromq/pyzmq","tags":["messaging","networking","asynchronous","ipc","distributed-systems"],"install":[{"cmd":"pip install pyzmq","lang":"bash","label":"Basic Install"}],"dependencies":[],"imports":[{"note":"The primary module to import for PyZMQ functionality is `zmq`, not `pyzmq`. The `pyzmq` package is installed, but its main API is exposed via the `zmq` module.","wrong":"import pyzmq","symbol":"zmq","correct":"import zmq"},{"note":"The `Context` manages the ZeroMQ runtime and should be created once per application or thread.","symbol":"Context","correct":"import zmq\ncontext = zmq.Context()"},{"note":"Sockets are created from a `Context` and are the primary means of sending and receiving messages. `zmq.REQ`, `zmq.REP`, `zmq.PUB`, `zmq.SUB` etc. define the socket type.","symbol":"Socket","correct":"import zmq\nsocket = context.socket(zmq.REQ)"}],"quickstart":{"code":"import zmq\n\n# Get PyZMQ and underlying ZeroMQ library versions\nprint(f\"PyZMQ version: {zmq.__version__}\")\nprint(f\"ZeroMQ library version: {zmq.zmq_version()}\\n\")\n\n# Create a ZeroMQ context, which manages connections and sockets\ncontext = zmq.Context()\n\n# Create a PUSH socket (a simple one-way message sender)\nsocket = context.socket(zmq.PUSH)\n\ntry:\n    # Bind the socket to a random ephemeral port on localhost\n    # In a real application, you'd bind/connect to a known address\n    port = socket.bind_to_random_port(\"tcp://127.0.0.1\")\n    print(f\"PUSH socket bound to tcp://127.0.0.1:{port}\")\n\n    # Send a message\n    message = \"Hello from PyZMQ!\"\n    socket.send_string(message)\n    print(f\"Sent message: '{message}'\")\n\nexcept zmq.ZMQError as e:\n    print(f\"ZeroMQ Error occurred: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\nfinally:\n    # Always close sockets and terminate the context to release resources\n    if socket:\n        socket.close()\n        print(\"Socket closed.\")\n    if context:\n        context.term()\n        print(\"Context terminated.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize PyZMQ, create a context and a PUSH socket, bind it to a local address, and send a simple string message. It also shows proper cleanup of sockets and context."},"warnings":[{"fix":"Always use `import zmq` to access PyZMQ's functionality.","message":"The PyPI package name is `pyzmq`, but the primary Python module to import is `zmq`. Attempting `import pyzmq` will not expose the core ZeroMQ API (though `pyzmq` itself is a valid, but less common, module).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `socket.poll()` to check for activity, or pass `zmq.NOBLOCK` flag to `send()`/`recv()`. When using `NOBLOCK`, be prepared to handle `zmq.Again` (EAGAIN) exceptions when an operation would block.","message":"By default, `socket.recv()` and `socket.send()` are blocking operations. If no message is available or the send buffer is full, your application can hang indefinitely.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement proper lifecycle management, typically using `try...finally` blocks or context managers, to ensure sockets are closed and the context is terminated.","message":"It is crucial to properly close all sockets with `socket.close()` and terminate the ZeroMQ context with `context.term()` to release system resources. Failing to do so can lead to resource leaks, hanging processes, or prevent proper application shutdown.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For asyncio, always use the `zmq.asyncio` module and its `Context`. Refer to the official PyZMQ documentation for the correct modern `asyncio` integration patterns.","message":"The integration with `asyncio` underwent significant changes around PyZMQ version 17.x. Older patterns of integrating with asyncio might no longer work or behave differently, particularly regarding event loop management and `asyncio.set_event_loop()`.","severity":"breaking","affected_versions":"Before 17.x, especially when upgrading to 17.x or later."},{"fix":"When needing to send multiple logical parts in a single ZeroMQ message, use `socket.send_multipart(list_of_bytes_or_strings)` and `socket.recv_multipart()`.","message":"ZeroMQ messages are 'framed' by the library, but sending multiple distinct parts requires using `socket.send_multipart()` and `socket.recv_multipart()`. Concatenating strings and sending with `send_string()` will be treated as a single message part.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T17:52:02.578Z","next_check":"2026-07-14T00:00:00.000Z","problems":[{"fix":"pip install pyzmq","cause":"The PyZMQ library, which provides the 'zmq' module, is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'zmq'"},{"fix":"Ensure all previous sockets are properly closed and unbound, or choose a different available port for binding.","cause":"The requested port for binding a ZeroMQ socket is already occupied by another process or a previously unclosed socket.","error":"zmq.error.ZMQError: Address already in use"},{"fix":"Verify the socket type (e.g., zmq.REQ, zmq.REP, zmq.PUB, zmq.SUB) matches the intended messaging pattern and the specific operation being performed.","cause":"An unsupported operation was attempted on a ZeroMQ socket type (e.g., trying to send on a SUB socket or recv on a PUB socket).","error":"zmq.error.ZMQError: Operation cannot be accomplished in current state"},{"fix":"Handle the zmq.Again exception by retrying the operation later, using socket.poll() to wait for events, or switching to blocking operations if appropriate.","cause":"A non-blocking socket operation (e.g., recv with zmq.NOBLOCK) was called when no data was immediately available, or a send buffer was full.","error":"zmq.error.Again"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":"27.1.0","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"24.9M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"24.9M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":1.9,"disk_size":"24.9M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.1,"import_time_s":0.03,"mem_mb":1.9,"disk_size":"22M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.9,"disk_size":"22M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.9,"disk_size":"22M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":1.9,"disk_size":"27.0M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.11,"mem_mb":1.9,"disk_size":"27.0M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.11,"mem_mb":1.9,"disk_size":"27.0M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.8,"import_time_s":0.08,"mem_mb":1.9,"disk_size":"24M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":1.9,"disk_size":"24M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":1.9,"disk_size":"24M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"18.8M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"18.8M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"18.8M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":0.11,"mem_mb":2.6,"disk_size":"16M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"16M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"16M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":2.8,"disk_size":"18.5M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.8,"disk_size":"18.4M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":2.8,"disk_size":"18.4M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.7,"import_time_s":0.08,"mem_mb":2.6,"disk_size":"15M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"15M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.6,"disk_size":"15M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"24.4M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"24.4M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"24.4M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.4,"import_time_s":0.04,"mem_mb":1.9,"disk_size":"21M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"21M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.9,"disk_size":"21M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":-1},{"runtime":"python:3.10-slim","exit_code":-1},{"runtime":"python:3.11-alpine","exit_code":-1},{"runtime":"python:3.11-slim","exit_code":-1},{"runtime":"python:3.12-alpine","exit_code":-1},{"runtime":"python:3.12-slim","exit_code":-1},{"runtime":"python:3.13-alpine","exit_code":-1},{"runtime":"python:3.13-slim","exit_code":-1},{"runtime":"python:3.9-alpine","exit_code":-1},{"runtime":"python:3.9-slim","exit_code":-1}]}}