Dynamically manage pools of asyncio tasks
Go to file
2022-05-05 08:35:32 +02:00
.github/workflows Fix cancel message bug for Python 3.8; test coverage workaround for Python version conditions 2022-04-10 10:43:53 +02:00
docs Add until_closed method to pools 2022-05-05 08:35:32 +02:00
requirements sphinx documentation; adjusted all docstrings; moved some modules to non-public subpackage 2022-03-24 13:38:30 +01:00
src/asyncio_taskpool Add until_closed method to pools 2022-05-05 08:35:32 +02:00
tests Add until_closed method to pools 2022-05-05 08:35:32 +02:00
usage made start "non-async" using meta task 2022-03-30 21:51:19 +02:00
.coveragerc Fix cancel message bug for Python 3.8; test coverage workaround for Python version conditions 2022-04-10 10:43:53 +02:00
.gitignore sphinx documentation; adjusted all docstrings; moved some modules to non-public subpackage 2022-03-24 13:38:30 +01:00
.readthedocs.yaml Fixed Python 3.8 compatibility bugs; classmethod+property workaround; control session buffer 2022-04-08 11:53:53 +02:00
cloc.sh Add workflows & badges 2022-04-03 16:33:28 +02:00
COPYING license & copyright notices; docstrings for each module; extended readme 2022-02-09 23:14:42 +01:00
COPYING.LESSER license & copyright notices; docstrings for each module; extended readme 2022-02-09 23:14:42 +01:00
coverage.sh Optimize coverage script/settings 2022-04-06 21:47:39 +02:00
pyproject.toml Initial commit 2022-02-03 20:06:30 +01:00
README.md Fixed Python 3.8 compatibility bugs; classmethod+property workaround; control session buffer 2022-04-08 11:53:53 +02:00
setup.cfg Add until_closed method to pools 2022-05-05 08:35:32 +02:00

asyncio-taskpool

GitHub last commit Lines of code Lines of comments Test coverage License: LGPL v3.0 PyPI version

Dynamically manage pools of asyncio tasks

Full documentation available at RtD.


Contents

Summary

A task pool is an object with a simple interface for aggregating and dynamically managing asynchronous tasks.

With an interface that is intentionally similar to the multiprocessing.Pool class from the standard library, the TaskPool provides you such methods as apply, map, and starmap to execute coroutines concurrently as asyncio.Task objects. There is no limitation imposed on what kind of tasks can be run or in what combination, when new ones can be added, or when they can be cancelled.

For a more streamlined use-case, the SimpleTaskPool provides an even more intuitive and simple interface at the cost of flexibility.

If you need control over a task pool at runtime, you can launch an asynchronous ControlServer to be able to interface with the pool from an outside process or via a network, and stop/start tasks within the pool as you wish.

Usage

Generally speaking, a task is added to a pool by providing it with a coroutine function reference as well as the arguments for that function. Here is what that could look like in the most simplified form:

from asyncio_taskpool import SimpleTaskPool
...
async def work(_foo, _bar): ...

async def main():
    pool = SimpleTaskPool(work, args=('xyz', 420))
    pool.start(5)
    ...
    pool.stop(3)
    ...
    await pool.gather_and_close()

Since one of the main goals of asyncio-taskpool is to be able to start/stop tasks dynamically or "on-the-fly", most of the associated methods are non-blocking most of the time. A notable exception is the gather_and_close method for awaiting the return of all tasks in the pool. (It is essentially a glorified wrapper around the asyncio.gather function.)

For working and fully documented demo scripts see USAGE.md.

Installation

pip install asyncio-taskpool

Dependencies

Python Version 3.8+, tested on Linux

Testing

Install coverage with pip, then execute the ./coverage.sh shell script to run all unit tests and save the coverage report.

License

asyncio-taskpool is licensed under the GNU LGPL version 3.0 specifically.

The full license texts for the GNU GPLv3.0 and the GNU LGPLv3.0 are included in this repository. If not, see https://www.gnu.org/licenses/.


© 2022 Daniil Fajnberg