zeromq / pyzmq

PyZMQ: Python bindings for zeromq
http://zguide.zeromq.org/py:all
BSD 3-Clause "New" or "Revised" License
3.69k stars 637 forks source link

DISH socket not working with asyncio context #1139

Open dragos-vlad opened 6 years ago

dragos-vlad commented 6 years ago

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 commented 6 years ago

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
minrk commented 6 years ago

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

dragos-vlad commented 6 years ago

@minrk How do I find out?

minrk commented 6 years ago

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.

dragos-vlad commented 6 years ago

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.

minrk commented 6 years ago

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

smalls12 commented 3 years ago

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?

minrk commented 3 years ago

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

alonbl commented 7 months ago

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

minrk commented 7 months ago

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.

alonbl commented 7 months ago

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

alonbl commented 7 months ago

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