An attempt at comingling async-code and nonasync-code-in-a-thread in an argonomic way.
Latest release 20241220:
- New async_iter(Iterable) returning an asynchronous iterator of a synchronous iterable.
- New amap(func,iterable) asynchronously mapping a function over an iterable.
One of the difficulties in adapting non-async code for use in an async world is that anything asynchronous needs to be turtles all the way down: a single blocking synchronous call anywhere in the call stack blocks the async event loop.
This module presently provides a pair of decorators for
asynchronous generators and functions which dispatches them in
a Thread and presents an async wrapper.
afunc(*da, **dkw)
A decorator for a synchronous function which turns it into an asynchronous function.
Example:
@afunc
def func(count):
time.sleep(count)
return count
slept = await func(5)
agen(*da, **dkw)
A decorator for a synchronous generator which turns it into an asynchronous generator. Exceptions in the synchronous generator are reraised in the asynchronous generator.
Example:
@agen
def gen(count):
for i in range(count):
yield i
time.sleep(1.0)
async for item in gen(5):
print(item)
amap(func: Callable[[Any], Any], it: Iterable, concurrent=False, unordered=False, indexed=False)
An asynchronous generator yielding the results of func(item)
for each item in the asynchronous iterable it.
If concurrent is False (the default), run each func(item)
call in series.
If concurrent is true run the function calls as asyncio
tasks concurrently.
If unordered is true (default False) yield results as
they arrive, otherwise yield results in the order of the items
in it, but as they arrive - tasks still evaluate concurrently
if concurrent is true.
If indexed is true (default False) yield 2-tuples of
(i,result) instead of just result, where i is the index
if each item from it counting from 0.
Example of an async function to fetch URLs in parallel.
async def get_urls(urls : List[str]):
""" Fetch `urls` in parallel.
Yield `(url,response)` 2-tuples.
"""
async for i, response in amap(
requests.get, urls,
concurrent=True, unordered=True, indexed=True,
):
yield urls[i], response
async_iter(it: Iterable)
Return an asynchronous iterator yielding items from the iterable it.
Release Log
Release 20241220:
- New async_iter(Iterable) returning an asynchronous iterator of a synchronous iterable.
- New amap(func,iterable) asynchronously mapping a function over an iterable.
Release 20241215:
- @afunc: now uses asyncio.to_thread() instead of wrapping @agen, drop the decorator parameters since no queue or polling are now used.
- @agen: nonpolling implementation - now uses asyncio.to_thread() for the next(genfunc) step, drop the decorator parameters since no queue or polling are now used.
Release 20241214.1: Doc update.
Release 20241214: Initial release with @agen and @afunc decorators.
Release files for cs-naysync 20241220
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cs_naysync-20241220.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cs_naysync-20241220-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.9 kB
Release files / cs_naysync-20241220.tar.gz
| Download URL | cs_naysync-20241220.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fb553c8339d77acd633d7e81daed121ce67c67cf9c7bf994a291765b21b9cd7a
|
|
BLAKE2b-256 checksum How to use checksums |
0dd3c7f887c3ab074a2a36b2fe94a0c4289d174c37dd4055f8cd0b0967e61fee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|
Release files / cs_naysync-20241220-py3-none-any.whl
| Download URL | cs_naysync-20241220-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
21678f3e7f9a3dc375cd98325df95f031310bb99a84094bd375c1ad68b591fb3
|
|
BLAKE2b-256 checksum How to use checksums |
80e205ba249f646c76d54d90633fd066b611131c9043bcdbadb537d706a60c8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|