Closed spchamp closed 3 months ago
This might be similar to the following https://github.com/zeromq/pyzmq/issues/1139
If client, server sockets may not available with zmq.asyncio, will look at other approaches. There's always pub/sub lol
Reading up more about this in the links from nr. 1139, if there may be socket types in zmq that don't use file descriptors in so much (??) and the application is itself using asyncio for coroutine dispatch, it might be just as well to use the synchronous API within these coroutines?
Do I understand that there might not be any blocking on FD I/O in the process or the thread, with these sockets specifically? client/server, radio/dish, and with inproc transports even?
The application is already a mashup of synchronous and async approaches LoL and it has something of a threading model, not worried about style I guess
Correct, the "threadsafe" group of socket types do not expose zmq.FD, so are therefore incompatible with event-loop integration via the zmq.asyncio
subclass. You can still use any sync sockets and avoid blocking the event-loop by always using DONTWAIT
in your send/recv and handle the waiting yourself with e.g. asyncio.sleep
:
import asyncio
import zmq
async def recv_multipart(sock, flags=0, copy=True, poll_interval=0.1):
while True:
try:
return sock.recv_multipart(flags | zmq.DONTWAIT, copy=copy)
except zmq.Again:
# can't wait natively, try again in a bit
await asyncio.sleep(poll_interval)
That's a coroutine that will block until recv returns, but won't block the event loop because send/recv with DONTWAIT always return immediately. You could also do a smarter wait with exponential backoff, jitter, timeout, etc.
You can also put blocking socket calls in a background thread via ThreadPoolExecutor, which is usually not safe, but it is specifically these "threadsafe" sockets that have this problem, so might be fine. It would be safe as long as all socket methods occur in the same thread.
Closing here as this is the same issue as #1139, but feel free to continue there.
This is a pyzmq bug
What pyzmq version?
25.1.2
What libzmq version?
4.3.4 (bundled)
Python version (and how it was installed)
Python 3.11.2, via openSUSE devel:languages:python factory
OS
openSUSE Leap 15.5
What happened?
When testing an application with ZMQ client and server sockets using
zmq.asyncio
, I'm seeing the messageAttributeError: FD attribute is write-only
during server socket initialization.After subsequent testing, this error occurs similarly with the
draft/client-server.py
example ported forzmq.asyncio
.After building pyzmq from source, I'm able to view a traceback presenting an earlier error, in a call within the cffi sections of pyzmq. The traceback is included below.
Code to reproduce bug
Traceback, if applicable
More info
Using Python installed from OpenSUSE devel:languages:python repository, openSUSE Build System: https://build.opensuse.org/project/show/devel:languages:python
In this newer installation for pyzmq, the installation uses the bundled libzmq from pyzmq, built with GCC at installation time, presumably using the same compiler settings as the openSUSE python build.
I'm not certain which libzmq was used in the earlier installation. Both were installed from PyPI for this same pyzmq version. The second installation was created with
pip install -vv --force --no-binary=pyzmq pyzmq