Skip to main content

Tools to run asyncio tasks concurrently.

Project description

asyncio-concurrent-tasks

tests version python

Tooling to run asyncio tasks.

Background task

Task that is running in the background until cancelled. Can be used as a context manager.

Example usage:

import asyncio
from typing import Callable, Awaitable
from concurrent_tasks import BackgroundTask


class HeartBeat(BackgroundTask):
    def __init__(self, interval: float, func: Callable[[], None]):
        super().__init__(self._run, interval, func)

    async def _run(self, interval: float, func: Callable[[], Awaitable]) -> None:
        while True:
            await func()
            await asyncio.sleep(interval)

Threaded task pool

Run async tasks in a dedicated thread. It will have its own event loop.

Parameters:

  • name will be used as the thread's name.
  • size can be a positive integer to limit the number of tasks concurrently running.
  • timeout can be set to define a maximum running time for each time after which it will be cancelled. Note: this excludes time spent waiting to be started (time spent in the buffer).
  • context_manager can be optional context managers that will be entered when the loop has started and exited before the loop is stopped.

💡 All tasks will be completed when the pool is stopped.

💡Blocking and async version are the same, prefer the async version if client code is async.

Blocking

This can be used to run async functions in a dedicated event loop, while keeping it running to handle background tasks

Example usage:

from concurrent_tasks import BlockingThreadedTaskPool


async def func():
    ...


with BlockingThreadedTaskPool() as pool:
    # Create and run the task.
    result = pool.run(func())
    # Create a task, the future will hold information about completion.
    future = pool.create_task(func())

Async

Threads can be useful in cooperation with asyncio to let the OS guarantee fair resource distribution between threads. This is especially useful in case you cannot know if called code will properly cooperate with the event loop.

Example usage:

from concurrent_tasks import AsyncThreadedTaskPool


async def func():
    ...


async with AsyncThreadedTaskPool() as pool:
    # Create and run the task.
    result = await pool.run(func())
    # Create a task, the future will hold information about completion.
    future = pool.create_task(func())

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

concurrent_tasks-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

concurrent_tasks-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file concurrent_tasks-0.1.0.tar.gz.

File metadata

  • Download URL: concurrent_tasks-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.6 Darwin/22.1.0

File hashes

Hashes for concurrent_tasks-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f3c91fa79ce7f39b9c93495d9e6c6d8eb350b5880ba5cc9d2ee7c85c46ac2d64
MD5 12c0570bdc70aa54da9944e47fafd0f7
BLAKE2b-256 faefaba432fa2f3d0d14d306768132c6ea5dbb842d2df52071e9f2afb0506378

See more details on using hashes here.

File details

Details for the file concurrent_tasks-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: concurrent_tasks-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.6 Darwin/22.1.0

File hashes

Hashes for concurrent_tasks-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e947b25006a0266e27e9012217124c66192969b22cad24c65c0eae6ba845a03
MD5 afebb8f392d441e72f1b3402dabc0cdb
BLAKE2b-256 9afbf56d021b2f69e148af7242be397e29a09ed92599293c434b366e566965f8

See more details on using hashes here.

Supported by

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