A pool of coroutine functions.
Project description
asyncio-pool-ng
About
AsyncioPoolNG takes the ideas used in asyncio-pool and wraps them around a TaskGroup from anyio.
AsyncioPool
has three main functions spawn
, map
, and itermap
.
spawn
: Schedule an async function on the pool and get a future back which will eventually have either the result or the exception from the function.map
: Spawn an async function for each item in an iterable object, and return a set containing a future for each item.
asyncio.wait()
can be used to wait for the set of futures to complete.- When the
AsyncioPool
closes, it will wait for all tasks to complete. All pending futures will be complete once it is closed.
itermap
: Works similarly tomap
but returns an Async Generator which yields each future as it completes.
Differences from asyncio-pool
asyncio-pool-ng
implements Python typing and passes validation checks with mypy's strict mode. This helps IDEs and static type checkers know what type of result to expect when getting data from a completed future.asyncio-pool
uses callbacks to process data before returning it;asyncio-pool-ng
only returns Future instances directly. The future will contain either a result or an exception which can then be handled as needed.- While
asyncio-pool
schedules Coroutine instances directly,asyncio-pool-ng
takes the callable and arguments, and creates the Coroutine instance at execution time.
Example
import asyncio
import logging
from random import random
from asyncio_pool import AsyncioPool
logging.basicConfig(level=logging.INFO)
async def worker(number: int) -> int:
await asyncio.sleep(random() / 2)
return number * 2
async def main() -> None:
result: int = 0
results: list[int] = []
async with AsyncioPool(2) as pool:
"""spawn task and wait for the results"""
result = await pool.spawn(worker, 5)
assert result == 10
logging.info(f"results for pool.spawn(worker, 5): {result}")
"""spawn task and get results later"""
future: asyncio.Future[int] = pool.spawn(worker, 5)
# do other stuff
result = await future
assert result == 10
"""map an async function to a set of values"""
futures: set[asyncio.Future[int]] = pool.map(worker, range(10))
await asyncio.wait(futures)
results = [x.result() for x in futures]
logging.info(f"results for pool.map(worker, range(10)): {results}")
results.sort()
assert results == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
"""iterate futures as they complete"""
logging.info("results for pool.itermap(worker, range(10)):")
results = []
async for future in pool.itermap(worker, range(10)):
results.append(future.result())
logging.info(f"> {future.result()}")
results.sort()
assert results == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
asyncio.run(main())
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
asyncio_pool_ng-0.7.2.tar.gz
(6.9 kB
view details)
Built Distribution
File details
Details for the file asyncio_pool_ng-0.7.2.tar.gz
.
File metadata
- Download URL: asyncio_pool_ng-0.7.2.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.5 Linux/4.19.104-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e19d1404e8cb3d7ea9b4c5b123636815d7cad67e13d10900a79f67ff287807d |
|
MD5 | 3ff994fc9be71c94ddb4c41dc12f5585 |
|
BLAKE2b-256 | 91034798e29b6df09af9f3a47c2d8cd3b3ba02dcb35ddb76a08a390498e2be70 |
File details
Details for the file asyncio_pool_ng-0.7.2-py3-none-any.whl
.
File metadata
- Download URL: asyncio_pool_ng-0.7.2-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.5 Linux/4.19.104-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7fb0d9d0d67d800d010472d25212e3e2bd91026f1973d1bdbaffa9da88a9abe |
|
MD5 | b4aa19d9f130c25f22def87aecf304dd |
|
BLAKE2b-256 | 7dc36432cb40962bc984ec7af3b94f4100dee36830e64cdff28b71658cbe8813 |