pyzmq icon indicating copy to clipboard operation
pyzmq copied to clipboard

DISH socket not working with asyncio context

Open dragos-vlad opened this issue 7 years ago • 16 comments

Hi,

I'm trying to initialise a DISH socket using zmq.asyncio.Context fails with:

File "zmq/backend/cython/socket.pyx", line 492, in zmq.backend.cython.socket.Socket.get
server_1  |   File "zmq/backend/cython/socket.pyx", line 264, in zmq.backend.cython.socket._getsockopt
server_1  |   File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
server_1  | zmq.error.ZMQError: Invalid argument

The code i'm using is:

ctx = zmq.asyncio.Context.instance()
ctx.socket(zmq.DISH)

Initialising the socket with zmq.Context works as intended.

System Info:

os: Debian 8
python: 3.6.3
pyzmq: 17.0.0.b4
libzmq: 4.2.3
Draft API available: True

dragos-vlad avatar Feb 09 '18 11:02 dragos-vlad

I'm also getting this exception when the application closes.

Exception ignored in: <bound method Socket.__del__ of <zmq.asyncio.Socket object at 0x7f3901bb4e18>>
 Traceback (most recent call last):
   File "/usr/local/lib/python3.6/site-packages/zmq/sugar/socket.py", line 66, in __del__
     self.close()
   File "/usr/local/lib/python3.6/site-packages/zmq/_future.py", line 157, in close
     self._clear_io_state()
   File "/usr/local/lib/python3.6/site-packages/zmq/asyncio/__init__.py", line 60, in _clear_io_state
     self.io_loop.remove_reader(self._fd)
   File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 342, in remove_reader
     return self._remove_reader(fd)
   File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 279, in _remove_reader
     key = self._selector.get_key(fd)
   File "/usr/local/lib/python3.6/selectors.py", line 189, in get_key
     return mapping[fileobj]
   File "/usr/local/lib/python3.6/selectors.py", line 70, in __getitem__
     fd = self._selector._fileobj_lookup(fileobj)
   File "/usr/local/lib/python3.6/selectors.py", line 224, in _fileobj_lookup
     return _fileobj_to_fd(fileobj)
   File "/usr/local/lib/python3.6/selectors.py", line 39, in _fileobj_to_fd
     "{!r}".format(fileobj)) from None
 ValueError: Invalid file object: None

dragos-vlad avatar Feb 09 '18 11:02 dragos-vlad

do DISH sockets not have zmq.FD? If so, then they cannot be used with asyncio.

minrk avatar Feb 09 '18 17:02 minrk

@minrk How do I find out?

dragos-vlad avatar Feb 09 '18 18:02 dragos-vlad

Looks like it was an explicit design choice in libzmq to not support ZMQ_FD (https://github.com/zeromq/libzmq/issues/2941) in new threadsafe sockets (server/client, radio/dish, scatter/gather). Until that's fixed, you won't be able to use these sockets with asyncio or tornado. I wouldn't recommend using any of the draft-api sockets in production.

minrk avatar Feb 12 '18 12:02 minrk

Like you mentioned in the libzmq issue it's pretty important for the final version of the sockets to expose a way for event loops to work with them. Until then I recommend we keep this issue open.

Thanks for your help.

EDIT: The docs should be updated in order to reflect the current status. If you like I could update them and make a pull request.

dragos-vlad avatar Feb 12 '18 13:02 dragos-vlad

Adding a note to draft support that libzmq draft sockets cannot be used with eventloops would be worthwhile. Thanks!

minrk avatar Feb 12 '18 14:02 minrk

Was poking around with RADIO/DISH and asyncio, stumbled onto this issue. I've gone through some of the genesis of the problem. Do we know if this is still an issue in libzmq?

smalls12 avatar Jun 03 '21 04:06 smalls12

Yes, still an issue and no indication of progress. It's a tricky problem and someone needs to implement it in libzmq.

minrk avatar Jun 07 '21 07:06 minrk

Hi, It would be very nice if this can be implemented or even some hints of how to implement/what to look into. Thanks,

alonbl avatar Feb 15 '24 08:02 alonbl

This is a feature request for libzmq to implement event-loop-supporting handles for sockets. Until they address that, there isn't really anything pyzmq or users can do, other than do blocking calls in background threads. If DISH sockets get ZMQ_FD support, it should work with asyncio without any changes to pyzmq. If they choose to go another way, it might require some code in pyzmq to deal with it. There is a proposal that sounds like it would work, to me, but someone has to implement it in libzmq.

minrk avatar Feb 15 '24 12:02 minrk

Thanks for explaining that @minrk, I will try to see if there is an open issue at libzmq side and create relationship.

alonbl avatar Feb 15 '24 12:02 alonbl

Found: https://github.com/zeromq/libzmq/issues/2941

alonbl avatar Feb 15 '24 12:02 alonbl