prevent redundant zeroMQ thread spawning by sharing a single context#473
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Python ZeroMQ integration to follow the “single context per process” ZeroMQ pattern, reducing redundant ZMQ I/O thread creation and per-port overhead (fixes #472).
Changes:
- Introduces a module-level shared
_zmq_contextfor all ZMQ ports. - Updates
ZeroMQPortcreation to use an injected shared context rather than creating a new context per port. - Updates
terminate_zmq()to close sockets first and then terminate the shared context once.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def _get_zmq_context(): | ||
| """Return the process-level shared ZMQ context, creating it on first call.""" | ||
| global _zmq_context | ||
| if _zmq_context is None or _zmq_context.closed: | ||
| _zmq_context = zmq.Context() | ||
| return _zmq_context |
There was a problem hiding this comment.
The new shared-context lifecycle (_get_zmq_context() lazy init + terminate_zmq() single term) isn’t covered by tests. Adding a unit test that asserts only one zmq.Context is created across multiple init_zmq_port calls, and that terminate_zmq resets/terminates the context (including the failure path where port creation raises and no port is registered) would help prevent regressions.
There was a problem hiding this comment.
Will write tests once this PR is discussed with mentors!
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@pradeeban @Rahuljagwani please have a look. |
Previously, zeroMQPort instantiated a new zmq.Context() for every port, which unintentionally spawned unnecessary I/O threads and increased resource overhead.
What this does:
reduces memory footprint and eliminated redundant background thread creation.
This fixes #472 .