Run asyncio and another event loop in the same thread
Project description
Trio, an alternate async framework for Python, supports a feature called “guest mode” where it can run in the same thread as another event loop. In guest mode, low-level I/O waiting occurs on a worker thread, but the threading is invisible to user code, and both event loops can interact with each other without any special synchronization.
This package implements guest mode for asyncio. It has one public function:
aioguest.start_guest_run( coro, *, run_sync_soon_threadsafe, done_callback, run_sync_soon_not_threadsafe=None, debug=None, loop_factory=None, )
This effectively starts a call to asyncio.run(coro) in parallel with the currently running (“host”) event loop. The debug parameter is passed to asyncio.run() if specified. On Python 3.11+, you can also supply a loop_factory which will be passed to asyncio.Runner().
The parameters run_sync_soon_threadsafe, done_callback, and (optionally) run_sync_soon_not_threadsafe tell aioguest how to interact with the host event loop. Someday aioguest will have documentation of its own. Until then, see the Trio documentation for details on these parameters, including an example.
start_guest_run() returns the main task of the new asyncio run, i.e., the asyncio.Task that wraps coro. It may also return None if the main task could not be determined, such as because asyncio.run() raised an exception before starting to execute coro. The main task is provided mostly for cancellation purposes; while you can also register callbacks upon its completion, the run is not necessarily finished at that point, because background tasks and async generators might still be in the process of finalization.
Exceptions noticed when starting up the asyncio run might either propagate out of start_guest_run() or be delivered to your done_callback, maybe even before start_guest_run() returns (it will return None in that case). In general, problems noticed by aioguest will propagate out of start_guest_run(), while problems noticed by asyncio will be delivered to your done_callback.
aioguest requires Python 3.8 or later. Out of the box it supports the default asyncio event loop implementation (only) on Linux, Windows, macOS, FreeBSD, and maybe others. It does not support operating systems that provide only select() or poll(); due to thread-safety considerations it needs an I/O abstraction where the OS kernel is involved in registrations, such as IOCP, epoll, or kqueue. Alternative Python-based event loops can likely be supported given modest effort if they use such an abstraction. Alternative C-based event loops (such as uvloop) present much more of a challenge because compiled code generally can’t be monkeypatched.
Development status
aioguest has been tested with a variety of toy examples and pathological cases, and its unit tests exercise full coverage. It hasn’t had a lot of exposure to real-world problems yet. Maybe you’d like to expose it to yours?
License
aioguest is licensed under your choice of the MIT or Apache 2.0 license. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aioguest-0.1.0.tar.gz
.
File metadata
- Download URL: aioguest-0.1.0.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0bad95272592703379ada3d69f442c73b2cb64075be21d8617049c75747904b |
|
MD5 | 080caf61984bc803341fd9027d49e48c |
|
BLAKE2b-256 | 777777d0701a2b13d202de41809e3942d98596f2c791aadd94f4d3bcf2791a4c |
File details
Details for the file aioguest-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: aioguest-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f993e173503638a61e8f51b5f5f71ed0b9cfdb782b208d3822d10904cd0af75 |
|
MD5 | 1e5f87997a58dbc721677309bd8bff72 |
|
BLAKE2b-256 | 90ddfbdcc568d3655c626f6f5e08b91eeeb1497208f012c12cc87abc5afbcd3c |