Aiofutures
General information
aiofutures provides tools to integrate an asynchronous code into your synchronous
application in a usual and easy way using standard library's concurrent.futures.Executor interface.
It may be useful when you want to:
- smoothly migrate your synchronous codebase to asynchronous style
- decrease a number of threads in your application
Replace this:
from concurrent.futures import ThreadPoolExecutor
def sync_get_user(user_id):
...
with ThreadPoolExecutor() as ex:
future = ex.submit(sync_get_user, user_id)
result = future.result()
With this:
from aiofutures import AsyncExecutor
async def async_get_user(user_id):
...
with AsyncExecutor() as ex:
future = ex.submit(async_get_user, user_id)
result = future.result()
The former spawns a lot of threads and experiences all cons of GIL, the latter spawns the only one async thread (check out notes).
Installation
You can install aiofutures using pip:
pip install aiofutures
Usage
Implicit initialization (global executor)
Set an environment variable AIOFUTURES_INIT to any value and use shortcuts from the library:
os.environ.setdefault('AIOFUTURES_INIT', '1')
from aiofutures import run_async
async def io_bound_task(seconds):
await asyncio.sleep(seconds)
return seconds
future = run_async(io_bound_task, 5)
print(future.result())
AIOFUTURES_INIT implicitly initializes a global AsyncExecutor and gives you an option to use
shortcuts run_async and sync_to_async.
Explicit initialization
Use an instance of the AsyncExecutor directly:
from aiofutures import AsyncExecutor
executor = AsyncExecutor()
future = executor.submit(io_bound_task, 5)
print(future.result())
In cases when you need to do IO synchronously within async tasks, you can use sync_to_async:
from aiofutures import AsyncExecutor, sync_to_async
executor = AsyncExecutor()
async def io_bound_task():
# or with the shortcut
# url = await sync_to_async(fetch_url_from_db_sync)
url = await executor.sync_to_async(fetch_url_from_db_sync)
data = await fetch_data(url)
return data
future = executor.submit(io_bound_task)
print(future.result())
NOTE: You can use sync_to_async within tasks running in the executor only.
UVLoop
To use with the high performance uvloop install it before initialization:
# install before the import for the global executor
import uvloop
uvloop.install()
from aiofutures import run_async
...
# or before an explicit initialization
import uvloop
from aiofutures import AsyncExecutor
uvloop.install()
executor = AsyncExecutor()
Notes
- Take into account that asyncio still (CPython3.13) resolves DNS in threads, not asynchronously
- Any blocking function will block the whole AsyncExecutor
Contribution
All suggestions are welcome!
Metadata
Release files for aiofutures 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| aiofutures-0.1.3.tar.gz | 5.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aiofutures-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.4 kB
Release files / aiofutures-0.1.3.tar.gz
| Download URL | aiofutures-0.1.3.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f765d9669cd72e2d6ff13a8cab6be5a884a9511e3ad327ba91666456fd5db89a
|
|
BLAKE2b-256 checksum How to use checksums |
27d5d12a20bae655d8f3e6e372025b03f3ed4ee967518895efa1532b697ec86e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.4 Darwin/23.4.0
|
Release files / aiofutures-0.1.3-py3-none-any.whl
| Download URL | aiofutures-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ec7ab11f4707754fbd8dd38fe7a974461d4b6c68ea3564e4e5b0df89bdf8622b
|
|
BLAKE2b-256 checksum How to use checksums |
cd7c87415ac33ca93f9807d6f0059b385ea3a25ac517051c40587e18e4a51669
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.4 Darwin/23.4.0
|
Security for aiofutures 0.1.3
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.