Skip to main content

A pool of coroutine functions.

Project description

asyncio-pool-ng

PyPI version Python Versions License: MIT Code style: black

About

AsyncioPoolNG takes the ideas used in asyncio-pool and wraps them around an asyncio.TaskGroup.

AsyncioPool has three main functions spawn, map, and itermap.

  1. 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.
  2. 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.
  1. itermap: Works similarly to map but returns an Async Generator which yields each future as it completes.

Differences from asyncio-pool

  1. 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.
  2. 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.
  3. 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


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.8.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

asyncio_pool_ng-0.8.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_pool_ng-0.8.0.tar.gz.

File metadata

  • Download URL: asyncio_pool_ng-0.8.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.2 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for asyncio_pool_ng-0.8.0.tar.gz
Algorithm Hash digest
SHA256 f65e59dbe3480db97f64c496ada8e228ba7462f267cc76b9e083401835ae0f4a
MD5 a35523c44f9c920b820c0646ac591ca4
BLAKE2b-256 50f8ef56d70f5be4245e3af21ea51a6841fe7c16a5d7b4cd1f0f7aaeb2a0f8e3

See more details on using hashes here.

File details

Details for the file asyncio_pool_ng-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: asyncio_pool_ng-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.2 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for asyncio_pool_ng-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5396c8bc29d2f7a27a36ebd70c8e1307ed9b57d9ffda2611664334a1c0627f5
MD5 7ea6e2772e55360361a9f8bbc1ad8162
BLAKE2b-256 8b3e1e6faca443db00767b44fd691e57d4d0fab131da0a528863b4604b18a307

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page