Is it possible to do something similar to gnome glib's GSource in asyncio?

Vitaly Krug vitaly.krug at gmail.com
Sat Mar 17 22:39:15 EDT 2018


I am looking for a way to get a callback each time before asyncio event
loop goes back to waiting on I/O (select, epoll, etc.). Documentation and
googling for the answer hasn't helped yet. I am looking for something
similar to gnome glib's GSource interface  (
https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#GSource
).

Here is my use case:

Sometimes it's necessary to wrap an asynchronous implementation in a
synchronous API. This is how Pika AMQP client's BlockingConnection adapter
is built on top of Pika's asynchronous SelectConnection adapter without
incurring the overhead of multi-threading.

The blocking wrapper makes calls into the asynchronous layer and runs the
event loop (pika's proprietary select/epoll/kqueue event loop) while
waiting for the asynchronous request to complete.

For no-reply (i.e., no-ack) type of requests, the blocking wrapper simply
waits for the write buffers to empty out before returning - for this, it
checks the size of the write buffer each time before event loop goes back
to waiting for I/O on select/epoll/kqueue. And if the blocking layer finds
that the write buffer has emptied, it stops the event loop, so control can
return to the user of the blocking interface.

So, I need to be able to do something similar in asyncio to facilitate
rebasing BlockingConnection on asyncio in Python3.

Thank you in advance.

Vitaly



More information about the Python-list mailing list