you can have a little async (as a treat)
asyncio-thread-runner allows you to run async code from sync code.
This is useful when you're doing some sync stuff, but:
- you also need to do some async stuff, without making everything async
- maybe the sync stuff is an existing application
- maybe you still want to use your favorite sync library
- or maybe you need just a little async, without having to pay the full price
Features:
- unlike asyncio.run(), it provides a long-lived event loop
- unlike asyncio.Runner, it runs in a dedicated thread, and you can use it from multiple threads
- it allows you to use async context managers and iterables from sync code
- check out this article for why these are useful
Usage:
$ pip install asyncio-thread-runner
>>> async def double(i):
... return i * 2
...
>>> from asyncio_thread_runner import ThreadRunner
>>> runner = ThreadRunner()
>>> runner.run(double(2))
4
Annotated example:
import aiohttp
from asyncio_thread_runner import ThreadRunner
# you can use ThreadRunner as a context manager,
# or call runner.close() when you're done with it
with ThreadRunner() as runner:
# aiohttp.ClientSession() should be used as an async context manager,
# enter_context() will exit the context on runner shutdown;
# because instantiating ClientSession requires a running event loop,
# we pass it as a factory instead of calling it in the main thread
session = runner.enter_context(aiohttp.ClientSession)
# session.get() returns an async context manager...
request = session.get('https://death.andgravity.com/asyncio-bridge')
# which we turn into a normal one with wrap_context()
with runner.wrap_context(request) as response:
# response.content is an async iterator;
# we turn it into a normal iterator with wrap_iter()
lines = list(runner.wrap_iter(response.content))
# "got 935 lines"
print('got', len(lines), 'lines')
Metadata
Release files for asyncio-thread-runner 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asyncio_thread_runner-1.0.tar.gz | 7.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asyncio_thread_runner-1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.7 kB
Release files / asyncio_thread_runner-1.0.tar.gz
| Download URL | asyncio_thread_runner-1.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9b3f6c30d3ee8ae5cca1a243b6bb872404bdbcd88ec39fbbffa7984b96493182
|
|
BLAKE2b-256 checksum How to use checksums |
03fd1b5b3452f9e7df89b0c9bd246d276e0ae3a6b6bd7304076e09f2df1bf9fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 19, 2025.
Transparency logRelease files / asyncio_thread_runner-1.0-py3-none-any.whl
| Download URL | asyncio_thread_runner-1.0-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d7cc3f8b2f8446ac9450132ae8372a4810734ea8399e20d4233cc43920d8c812
|
|
BLAKE2b-256 checksum How to use checksums |
8d871fead469bb900643e2962aaf49b2b69f0e3222b652fd78d3a8df6fbb0e17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 19, 2025.
Transparency log