Skip to main content

aio_process_pool

PyPI - Version PyPI - Python Version


A simple async, android compatible process pool and a (mostly) concurrent.futures.Executor / ProcessPoolExecutor compliant Executor.

Not thread safe.

Table of Contents

Installation

pip install aio_process_pool

Usage

import asyncio

from aio_process_pool import ProcessPool, Executor

def foo(x=7):
    return x

# sync
executor = Executor()
assert executor.map(foo, [1, 2, 3]) == [1, 2, 3]
executor.shutdown()

# async
async def example():
    pool = ProcessPool()
    executor = Executor()

    # run a function in the pool
    assert await pool.run(foo, 2) == 2
    # map using a process pool
    assert await pool.map(foo, [1, 2, 3]) == [1, 2, 3]

    # use the executor with run_in_executor
    loop = asyncio.get_event_loop()
    assert await loop.run_in_executor(executor, foo) == 7

    # use submit
    concurrent_future = executor.submit(foo, 3)
    assert await asyncio.wrap_future(concurrent_future) == 3

    # map again
    assert await executor.map_async(foo, [1, 2, 3]) == [1, 2, 3]

    pool.shutdown()
    await executor.shutdown_async()

asyncio.run(example())

Executor

The Executor is mostly concurrent.futures.Executor compliant and can therefor be used as a replacement for concurrent.futures.ProcessPoolExecutor.

It is possible to monkey patch this executor into an environment:

import concurrent.futures, aio_process_pool
concurrent.futures.ProcessPoolExecutor = aio_process_pool.Executor

This is handy if you have code using a ProcessPoolExecutor and want to run it on android (with buildozer).

shutdown behaviour / deadlock under certain conditions

Since this package is based on asyncio I was not able to implement the specified shutdown behaviour under certain conditions.

If there are tasks pending and the wait parameter is True shutdown is supposed to block until all pending tasks are done. Since the execution of those task depends on the event loop this produces a deadlock.

The handle this situation this implementation raises a RuntimeError in that case instead of deadlocking.

If possible use shutdown_async instead. shutdown_async should behave concurrent.futures.Executor compliant.

map from within the loop

Since map is -- according to concurrent.futures.Executor -- supposed to be a sync function it uses asyncio.new_event_loop to get a new event loop and runs map_async in that loop. This is only possible if we're not inside a loop already. This is somehow not concurrent.futures.Executor compliant.

If possible use map_async instead.

If anyone knows how to improve this.....

Demo

import asyncio
from aio_process_pool import Executor

def fib(n):
    if n <= 2: return 1
    return fib(n-1) + fib(n-2)

def fib_wrapper(n):
    print(f"fib({n}) = .....")
    result = fib(n)
    print(f"fib({n}) = {result}")

async def watch_htop_and_output_while_execution():
    exe = Executor()
    await exe.map_async(fib_wrapper, range(45))
    exe.shutdown()

asyncio.run(watch_htop_and_output_while_execution())

License

aio_process_pool is distributed under the terms of the MIT license.

Release files for aio_process_pool 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio_process_pool 0.1.0
File Size Uploaded
aio_process_pool-0.1.0.tar.gz 18.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aio_process_pool 0.1.0
File Interpreter ABI Platform
aio_process_pool-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.7 kB

Release files / aio_process_pool-0.1.0.tar.gz

Download URL aio_process_pool-0.1.0.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
165a92b002fa976cb097fb889b76839b2f240036c671b9e73a5e3e2ace6312f1
BLAKE2b-256 checksum
How to use checksums
83403244bfe360d951a8a8150329707cbcfdc1c26285b65c88cb55d405c5bf56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / aio_process_pool-0.1.0-py3-none-any.whl

Download URL aio_process_pool-0.1.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e22ce26b26c496b1c4332a70705b862467cada9ccb7601a477f96ef9738fc7f
BLAKE2b-256 checksum
How to use checksums
5f303d1635d9996068bee77961b9a6e7465fc65eac98d7b9569675d2fd37c1a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page