Run async code from sync code.
Project description
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')
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asyncio_thread_runner-1.0.tar.gz.
File metadata
- Download URL: asyncio_thread_runner-1.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b3f6c30d3ee8ae5cca1a243b6bb872404bdbcd88ec39fbbffa7984b96493182
|
|
| MD5 |
72f86f28aee0b175c7d54613770cb0db
|
|
| BLAKE2b-256 |
03fd1b5b3452f9e7df89b0c9bd246d276e0ae3a6b6bd7304076e09f2df1bf9fb
|
Provenance
The following attestation bundles were made for asyncio_thread_runner-1.0.tar.gz:
Publisher:
publish.yaml on lemon24/asyncio-thread-runner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asyncio_thread_runner-1.0.tar.gz -
Subject digest:
9b3f6c30d3ee8ae5cca1a243b6bb872404bdbcd88ec39fbbffa7984b96493182 - Sigstore transparency entry: 409877555
- Sigstore integration time:
-
Permalink:
lemon24/asyncio-thread-runner@9241507e0d058c7c069ebb9cfd5277573c3dd044 -
Branch / Tag:
refs/tags/1.0 - Owner: https://github.com/lemon24
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@9241507e0d058c7c069ebb9cfd5277573c3dd044 -
Trigger Event:
push
-
Statement type:
File details
Details for the file asyncio_thread_runner-1.0-py3-none-any.whl.
File metadata
- Download URL: asyncio_thread_runner-1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7cc3f8b2f8446ac9450132ae8372a4810734ea8399e20d4233cc43920d8c812
|
|
| MD5 |
8a2daaf32a187b4a72cde07ab44caf53
|
|
| BLAKE2b-256 |
8d871fead469bb900643e2962aaf49b2b69f0e3222b652fd78d3a8df6fbb0e17
|
Provenance
The following attestation bundles were made for asyncio_thread_runner-1.0-py3-none-any.whl:
Publisher:
publish.yaml on lemon24/asyncio-thread-runner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asyncio_thread_runner-1.0-py3-none-any.whl -
Subject digest:
d7cc3f8b2f8446ac9450132ae8372a4810734ea8399e20d4233cc43920d8c812 - Sigstore transparency entry: 409877597
- Sigstore integration time:
-
Permalink:
lemon24/asyncio-thread-runner@9241507e0d058c7c069ebb9cfd5277573c3dd044 -
Branch / Tag:
refs/tags/1.0 - Owner: https://github.com/lemon24
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@9241507e0d058c7c069ebb9cfd5277573c3dd044 -
Trigger Event:
push
-
Statement type: