{"id":13721107,"url":"https://github.com/dano/aioprocessing","last_synced_at":"2025-05-07T13:31:40.756Z","repository":{"id":19132066,"uuid":"22361744","full_name":"dano/aioprocessing","owner":"dano","description":"A Python 3.5+ library that integrates the multiprocessing module with asyncio","archived":false,"fork":false,"pushed_at":"2022-09-16T02:30:02.000Z","size":136,"stargazers_count":655,"open_issues_count":6,"forks_count":34,"subscribers_count":21,"default_branch":"master","last_synced_at":"2024-11-14T10:38:29.467Z","etag":null,"topics":["asyncio","coroutines","multiprocessing","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-28T23:48:40.000Z","updated_at":"2024-11-12T08:22:41.000Z","dependencies_parsed_at":"2022-08-18T15:22:27.269Z","dependency_job_id":null,"html_url":"https://github.com/dano/aioprocessing","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dano%2Faioprocessing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dano%2Faioprocessing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dano%2Faioprocessing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dano%2Faioprocessing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dano","download_url":"https://codeload.github.com/dano/aioprocessing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252886342,"owners_count":21819703,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","coroutines","multiprocessing","python"],"created_at":"2024-08-03T01:01:12.518Z","updated_at":"2025-05-07T13:31:40.423Z","avatar_url":"https://github.com/dano.png","language":"Python","readme":"aioprocessing\n=============\n[![Build Status](https://github.com/dano/aioprocessing/workflows/aioprocessing%20tests/badge.svg?branch=master)](https://github.com/dano/aioprocessing/actions)\n\n\n`aioprocessing` provides asynchronous, [`asyncio`](https://docs.python.org/3/library/asyncio.html) compatible, coroutine \nversions of many blocking instance methods on objects in the [`multiprocessing`](https://docs.python.org/3/library/multiprocessing.html) \nlibrary. To use [`dill`](https://pypi.org/project/dill) for universal pickling, install using `pip install aioprocessing[dill]`. Here's an example demonstrating the `aioprocessing` versions of \n`Event`, `Queue`, and `Lock`:\n\n```python\nimport time\nimport asyncio\nimport aioprocessing\n\n\ndef func(queue, event, lock, items):\n    \"\"\" Demo worker function.\n\n    This worker function runs in its own process, and uses\n    normal blocking calls to aioprocessing objects, exactly \n    the way you would use oridinary multiprocessing objects.\n\n    \"\"\"\n    with lock:\n        event.set()\n        for item in items:\n            time.sleep(3)\n            queue.put(item+5)\n    queue.close()\n\n\nasync def example(queue, event, lock):\n    l = [1,2,3,4,5]\n    p = aioprocessing.AioProcess(target=func, args=(queue, event, lock, l))\n    p.start()\n    while True:\n        result = await queue.coro_get()\n        if result is None:\n            break\n        print(\"Got result {}\".format(result))\n    await p.coro_join()\n\nasync def example2(queue, event, lock):\n    await event.coro_wait()\n    async with lock:\n        await queue.coro_put(78)\n        await queue.coro_put(None) # Shut down the worker\n\nif __name__ == \"__main__\":\n    loop = asyncio.get_event_loop()\n    queue = aioprocessing.AioQueue()\n    lock = aioprocessing.AioLock()\n    event = aioprocessing.AioEvent()\n    tasks = [\n        asyncio.ensure_future(example(queue, event, lock)), \n        asyncio.ensure_future(example2(queue, event, lock)),\n    ]\n    loop.run_until_complete(asyncio.wait(tasks))\n    loop.close()\n```\n\nThe aioprocessing objects can be used just like their multiprocessing\nequivalents - as they are in `func` above - but they can also be \nseamlessly used inside of `asyncio` coroutines, without ever blocking\nthe event loop.\n\n\nWhat's new\n----------\n`v2.0.1`\n- Fixed a bug that kept the `AioBarrier` and `AioEvent` proxies returned from `AioManager` instances from working. Thanks to Giorgos Apostolopoulos for the fix.\n\n`v2.0.0`\n\n- Add support for universal pickling using [`dill`](https://github.com/uqfoundation/dill), installable with `pip install aioprocessing[dill]`. The library will now attempt to import [`multiprocess`](https://github.com/uqfoundation/multiprocess), falling back to stdlib `multiprocessing`. Force stdlib behaviour by setting a non-empty environment variable `AIOPROCESSING_DILL_DISABLED=1`. This can be used to avoid [errors](https://github.com/dano/aioprocessing/pull/36#discussion_r631178933) when attempting to combine `aioprocessing[dill]` with stdlib `multiprocessing` based objects like `concurrent.futures.ProcessPoolExecutor`.\n\n\nHow does it work?\n-----------------\n\nIn most cases, this library makes blocking calls to `multiprocessing` methods\nasynchronous by executing the call in a [`ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor), using\n[`asyncio.run_in_executor()`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.BaseEventLoop.run_in_executor). \nIt does *not* re-implement multiprocessing using asynchronous I/O. This means \nthere is extra overhead added when you use `aioprocessing` objects instead of \n`multiprocessing` objects, because each one is generally introducing a\n`ThreadPoolExecutor` containing at least one [`threading.Thread`](https://docs.python.org/2/library/threading.html#thread-objects). It also means \nthat all the normal risks you get when you mix threads with fork apply here, too \n(See http://bugs.python.org/issue6721 for more info).\n\nThe one exception to this is `aioprocessing.AioPool`, which makes use of the \nexisting `callback` and `error_callback` keyword arguments in the various \n[`Pool.*_async`](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool.apply_async) methods to run them as `asyncio` coroutines. Note that \n`multiprocessing.Pool` is actually using threads internally, so the thread/fork\nmixing caveat still applies.\n\nEach `multiprocessing` class is replaced by an equivalent `aioprocessing` class,\ndistinguished by the `Aio` prefix. So, `Pool` becomes `AioPool`, etc. All methods\nthat could block on I/O also have a coroutine version that can be used with `asyncio`. For example, `multiprocessing.Lock.acquire()` can be replaced with `aioprocessing.AioLock.coro_acquire()`. You can pass an `asyncio` EventLoop object to any `coro_*` method using the `loop` keyword argument. For example, `lock.coro_acquire(loop=my_loop)`.\n\nNote that you can also use the `aioprocessing` synchronization primitives as replacements \nfor their equivalent `threading` primitives, in single-process, multi-threaded programs \nthat use `asyncio`.\n\n\nWhat parts of multiprocessing are supported?\n--------------------------------------------\n\nMost of them! All methods that could do blocking I/O in the following objects\nhave equivalent versions in `aioprocessing` that extend the `multiprocessing`\nversions by adding coroutine versions of all the blocking methods.\n\n- `Pool`\n- `Process`\n- `Pipe`\n- `Lock`\n- `RLock`\n- `Semaphore`\n- `BoundedSemaphore`\n- `Event`\n- `Condition`\n- `Barrier`\n- `connection.Connection`\n- `connection.Listener`\n- `connection.Client`\n- `Queue`\n- `JoinableQueue`\n- `SimpleQueue`\n- All `managers.SyncManager` `Proxy` versions of the items above (`SyncManager.Queue`, `SyncManager.Lock()`, etc.).\n\n\nWhat versions of Python are compatible?\n---------------------------------------\n\n`aioprocessing` will work out of the box on Python 3.5+.\n\nGotchas\n-------\nKeep in mind that, while the API exposes coroutines for interacting with\n`multiprocessing` APIs, internally they are almost always being delegated\nto a `ThreadPoolExecutor`, this means the caveats that apply with using\n`ThreadPoolExecutor` with `asyncio` apply: namely, you won't be able to\ncancel any of the coroutines, because the work being done in the worker\nthread can't be interrupted.\n","funding_links":[],"categories":["并行","Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdano%2Faioprocessing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdano%2Faioprocessing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdano%2Faioprocessing/lists"}