akitchensyncio
Utility functions for asyncio which Stephan wished where in the stdlib but aren’t.
Requires a Python version which supports the async syntax (Python 3.5 or higher).
Installation
To install akitchensyncio, simply:
$ pip install akitchensyncio
Function wrap_future(f)
Takes a callable f which returns an awaitable, and returns a callable which wraps the awaitable in asyncio.ensure_future.
Can also be used as a decorator, especially with coroutine functions:
@wrap_future
async def foo(arg1, arg2):
...
This is especially useful in combination with functools.lru_cache. Suppose you have a coroutine function which does an asynchronous query, and you decide you want to introduce some caching. Just add two decorators as follows.
@functools.lru_cache(100)
@wrap_future
async def do_some_query(arg1, arg2):
...
Function transform_future(f, awaitable)
Apply a function to the result of an awaitable, return a future which delivers the result.
As an example, suppose you have a way to query addresses given names. The API takes a bunch of names rather than a single one to reduce overhead. However, to your callers you would like to hand out futures representing results for individual names.
Essentially you want to turn a “future resulting in a dict” into a “dict containing futures”. Kind of the opposite of async.gather.
from operator import itemgetter
def query_addresses(names):
fut = do_bunched_address_query(names)
# fut is a single future which resolves
# into a dict mapping names to addresses.
return {name: transform_future(itemgetter(name), fut) for name in names}
Function iawait(awaitable)
“Interactive await” – Run default eventloop until awaitable has completed. Mainly useful for interactive experimentation.
Then remove the “i” from iawait to get code which you can use in an async def function.
An alternative is to put this in your ~/.pythonrc.py:
def iawait(x):
import asyncio
loop = asyncio.get_event_loop()
return loop.run_until_complete(x)
This will only import asyncio on first use of iawait, so it won’t slow down your startup in general.
Metadata
Release files for akitchensyncio 0.1.1
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 | Actions |
|---|---|---|---|---|
| akitchensyncio-0.1.1.tar.gz | 5.1 kB | Without provenance | Details for akitchensyncio-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| akitchensyncio-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for akitchensyncio-0.1.1-py3-none-any.whl |
Total release size: 10.2 kB
Release files / akitchensyncio-0.1.1.tar.gz
| Download URL | akitchensyncio-0.1.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e416f5e36d27f5df8c52726b0691b55455dbbdbe97369b586a506cad25cf6eb4
|
|
BLAKE2b-256 checksum How to use checksums |
c951f971b7b4a0d2b60dbcfd9f9c443b93d391dafb5b818f802b424fab8bfed5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / akitchensyncio-0.1.1-py3-none-any.whl
| Download URL | akitchensyncio-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
58fb85a72d44f5e34d0ed11d13fa5ba5ce4388c084988f0c09192bbc715d1be5
|
|
BLAKE2b-256 checksum How to use checksums |
1d1bef17daf79bc8162a1dd3259988abda8af8ef7b9650a06cb1cb9bc923bdaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for akitchensyncio 0.1.1
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.