Comments (3)
Okay that's very interesting, I didn't know that was how python futures behave.
In the python docs, it does say future's callbacks are scheduled with call_soon
, so wouldn't get called when you await the result; so apparently the correct behavior. I opened this discussion on discuss.python.org to change it to match how tasks behave instead (where done callback is completed by the time await returns). Probably won't come to anything.
from pyzmq.
Confirming this is specific to asyncio and an always-busy socket. Trading zmq.asyncio.Context()
for zmq.Context()
in the receiver results in no memory growth beyond ~45MB. Similarly, if there's ever a pause between messages, references will be cleaned up.
The underlying issue is that in the example given, the event loop is never idle, so Future done_callbacks
are never called (the job of the event loop), which cleanup references to the Future, which in turn contains a reference to the message.
Adding an asyncio.sleep(0)
(e.g. every 1000 messages) gives the event loop a moment to do its usual cleanup. I'm surprised asyncio never calls the done_callbacks in the await
, but that's a question for asyncio. pyzmq isn't in control of the disposal of Futures, that's the event loop's job.
Ultimately, this is a result of the optimization that pyzmq doesn't use coroutines, it directly uses Futures. When pyzmq is busy (i.e. there's always a message to recv), these Futures are immediately complete, meaning the event loop doesn't idle if a zmq socket is the only thing that's ever awaited and zmq never actually has to wait for a message. If there were any actually async calls, cleanup would occur, which is why asyncio.sleep(0)
works.
This issue exists in general, but this particular reference can be avoided by not registering the done callback in the event (#1929). But if you actually have any other coroutines waiting to run, this busy coroutine will never yield to them as long as the socket has messages to receive.
You can see the same effect with pure asyncio:
import asyncio
from functools import partial
def printer(msg, _f):
print(msg)
async def main():
print("start")
for i in range(10):
digit = str(i % 10)
data = digit.encode("ascii") * 1_000_000
f = asyncio.Future()
f.add_done_callback(partial(printer, f"done_callback {i}"))
f.set_result(data)
print(f"await {i}")
# f is already done! this await doesn't yield to anything
await f
print("end")
asyncio.run(main())
which produces:
start
await 0
await 1
await 2
await 3
await 4
await 5
await 6
await 7
await 8
await 9
end
done_callback 0
done_callback 1
done_callback 2
done_callback 3
done_callback 4
done_callback 5
done_callback 6
done_callback 7
done_callback 8
done_callback 9
from pyzmq.
Yeah, I'm just surprised that the presence of await
doesn't give call_soon any chance to fire. I thought you would have to avoid the await, like:
f = s.recv()
if f.done():
result = f.result()
else:
result = await f
to starve other tasks.
This isn't the first time starvation has been an issue, but the performance benefit is substantial, if I recall. It's a tricky trade-off. I've thought perhaps a flag on the sockets to have a more polite, cooperative per-call yield could be desirable, but folks are going to have to find it first, if it's opt-in.
from pyzmq.
Related Issues (20)
- BUG: Asyncio cancellation race condition leaves Future exception never retrieved for recv_string() HOT 2
- Not able to build wheels for pyzmq HOT 12
- BUG: pyzmq v26 installation fails (build from sources) HOT 8
- BUG: Error compiling wheels using crossenv HOT 20
- BUG: IPC Support on Windows broken in 25.1.2 HOT 6
- BUG: Too many resets on loopback after socket.connect() call HOT 2
- DOC: Generic zmq.Context is unclear HOT 10
- BUG: PUBHandler is not thread-safe when using with async Context HOT 6
- BUG: (?) zmq.SERVER socket creation failing with zmq.asyncio, in zmq.backend.cython.checkrc._check_rc and AttributeError: FD attribute is write-only HOT 4
- BUG: ZMQ.HELLO_MSG HOT 4
- BUG: zmq.ROUTER_NOTIFY HOT 1
- BUG: pyzmq v26.0.0 installation fails on Bullseye (build from sources) HOT 22
- BUG: pyzmq 26.0.1 failed to install HOT 11
- pyzmq 26, Windows 11: Bad file descriptor HOT 10
- BUG: "No module named cython" when building 26.0.0+ with Pyston 2.3.5 HOT 5
- BUG: Build failure with PyPy3.10 and GCC 14, due to `error: initialization of โvoid **โ from incompatible pointer type โmutex_t **โ` HOT 2
- Asyncio examples must be updated
- 26.0.3 [BUG]: pytest fails in zmq/backend/cython/_zmq.py in `cython.compiled` import HOT 5
- Stopped installing in imx6ul board with multiple errors HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyzmq.