aioprocessing

2.0.1 · active · verified Thu Apr 16

aioprocessing is a Python 3.5+ library that provides asynchronous, asyncio-compatible versions of many blocking instance methods found in Python's standard `multiprocessing` module. It allows seamless integration of multiprocessing objects within `asyncio` coroutines without blocking the event loop. The library is currently at version 2.0.1 and generally follows an active release cadence, with the last major update (2.0.0) introducing `dill` support and internal `async/await` usage.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `AioProcess`, `AioQueue`, `AioLock`, and `AioEvent` from `aioprocessing` within an `asyncio` application. A worker function runs in a separate process, interacting with shared `aioprocessing` primitives, while the main `asyncio` loop communicates with it without blocking. Coroutine versions of blocking methods are prefixed with `coro_` (e.g., `coro_get`, `coro_put`, `coro_wait`, `coro_acquire`).

import asyncio
import time
import aioprocessing

def worker_func(queue, event, lock, items):
    """ Demo worker function for a separate process. """
    with lock:
        event.set()
    for item in items:
        time.sleep(0.1) # Simulate work
        queue.put(item + 5)
    queue.put(None) # Signal completion

async def example():
    queue = aioprocessing.AioQueue()
    lock = aioprocessing.AioLock()
    event = aioprocessing.AioEvent()
    items_to_process = [1, 2, 3, 4, 5]

    print("Starting worker process...")
    p = aioprocessing.AioProcess(target=worker_func, args=(queue, event, lock, items_to_process))
    p.start()

    # Wait for the worker to signal it's ready
    await event.coro_wait()
    print("Worker is ready.")

    async with lock: # Acquire lock in async context
        print("Lock acquired in main process (async).")
        # Demonstrating put from main process
        await queue.coro_put(78)
        print("Put 78 into queue from main process.")

    print("Collecting results from queue...")
    while True:
        result = await queue.coro_get()
        if result is None:
            break
        print(f"Got result: {result}")

    await p.coro_join() # Asynchronously wait for process to finish
    print("Worker process finished.")

if __name__ == "__main__":
    asyncio.run(example())

view raw JSON →