zeromq/pyzmq — pull request #251 by sdiehl (2012-08-16 01:00:02+00:00)
Title: Monitor

This a naive attempt to add support for ``zmq_ctx_set_monitor`` callbacks from libzmq 3.2

http://api.zeromq.org/3-3:zmq-ctx-set-monitor

The implementation is basically a global state hack because we couldn't modify libzmq to add a slot for an extra argument to pass a void pointer in and do the usual tricks. So it maintains a global mapping of libzmq socket handler pointers and looks up the associated pyzmq Context object and then in the C callback derefs the void pointer of the socket and looks up the associated Python callback.

See here: https://github.com/zeromq/libzmq/blob/master/src/ctx.cpp#L405

Kind of ugly, but it at least seems to work and I believe should be threadsafe since we acquire the GIL in the callback we don't pass sockets between threads.

Perhaps someone knows a cleaner way of implementing this feature?