All Projects → dano → Aioprocessing

dano / Aioprocessing

Licence: other
A Python 3.4+ library that integrates the multiprocessing module with asyncio

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Aioprocessing

Pulsar
Event driven concurrent framework for Python
Stars: ✭ 1,867 (+326.26%)
Mutual labels:  asyncio, multiprocessing
Aiomultiprocess
Take a modern Python codebase to the next level of performance.
Stars: ✭ 1,070 (+144.29%)
Mutual labels:  asyncio, multiprocessing
FinanceCenter
Fetching Financial Data (US/China)
Stars: ✭ 26 (-94.06%)
Mutual labels:  multiprocessing, asyncio
Python Concurrency
Code examples from my toptal engineering blog article
Stars: ✭ 131 (-70.09%)
Mutual labels:  asyncio, multiprocessing
Fooproxy
稳健高效的评分制-针对性- IP代理池 + API服务,可以自己插入采集器进行代理IP的爬取,针对你的爬虫的一个或多个目标网站分别生成有效的IP代理数据库,支持MongoDB 4.0 使用 Python3.7(Scored IP proxy pool ,customise proxy data crawler can be added anytime)
Stars: ✭ 195 (-55.48%)
Mutual labels:  asyncio, multiprocessing
think-async
🌿 Exploring cooperative concurrency primitives in Python
Stars: ✭ 178 (-59.36%)
Mutual labels:  multiprocessing, asyncio
distex
Distributed process pool for Python
Stars: ✭ 101 (-76.94%)
Mutual labels:  multiprocessing, asyncio
Pytorch A3c
Simple A3C implementation with pytorch + multiprocessing
Stars: ✭ 364 (-16.89%)
Mutual labels:  multiprocessing
Django Private Chat
Django one-to-one Websocket-based Asyncio-handled chat, developed by Bearle team
Stars: ✭ 376 (-14.16%)
Mutual labels:  asyncio
Picoweb
Really minimal web application framework for the Pycopy project (minimalist Python dialect) and its "uasyncio" async framework
Stars: ✭ 361 (-17.58%)
Mutual labels:  asyncio
Stig
TUI and CLI for the BitTorrent client Transmission
Stars: ✭ 360 (-17.81%)
Mutual labels:  asyncio
Requests Threads
🎭 Twisted Deferred Thread backend for Requests.
Stars: ✭ 366 (-16.44%)
Mutual labels:  asyncio
Uvicorn
The lightning-fast ASGI server. 🦄
Stars: ✭ 4,676 (+967.58%)
Mutual labels:  asyncio
Github Stats
Better GitHub statistics images for your profile, no external server required
Stars: ✭ 338 (-22.83%)
Mutual labels:  asyncio
Plutus
An automated bitcoin wallet collider that brute forces random wallet addresses
Stars: ✭ 404 (-7.76%)
Mutual labels:  multiprocessing
Unifiedmessagerelay
Group Message Forward Framework (supports QQ Telegram Line Discord)
Stars: ✭ 363 (-17.12%)
Mutual labels:  asyncio
Aiosqlite
asyncio bridge to the standard sqlite3 module
Stars: ✭ 411 (-6.16%)
Mutual labels:  asyncio
Ring
Python cache interface with clean API and built-in memcache & redis + asyncio support.
Stars: ✭ 404 (-7.76%)
Mutual labels:  asyncio
Aiotg
Asynchronous Python library for building Telegram bots
Stars: ✭ 374 (-14.61%)
Mutual labels:  asyncio
Lahja
Lahja is a generic multi process event bus implementation written in Python 3.6+ to enable lightweight inter-process communication, based on non-blocking asyncio
Stars: ✭ 374 (-14.61%)
Mutual labels:  asyncio

aioprocessing

Build Status

aioprocessing provides asynchronous, asyncio compatible, coroutine versions of many blocking instance methods on objects in the multiprocessing library. Here's an example demonstrating the aioprocessing versions of Event, Queue, and Lock:

import time
import asyncio
import aioprocessing


def func(queue, event, lock, items):
    """ Demo worker function.

    This worker function runs in its own process, and uses
    normal blocking calls to aioprocessing objects, exactly 
    the way you would use oridinary multiprocessing objects.

    """
    with lock:
        event.set()
        for item in items:
            time.sleep(3)
            queue.put(item+5)
    queue.close()


async def example(queue, event, lock):
    l = [1,2,3,4,5]
    p = aioprocessing.AioProcess(target=func, args=(queue, event, lock, l))
    p.start()
    while True:
        result = await queue.coro_get()
        if result is None:
            break
        print("Got result {}".format(result))
    await p.coro_join()

async def example2(queue, event, lock):
    await event.coro_wait()
    async with lock:
        await queue.coro_put(78)
        await queue.coro_put(None) # Shut down the worker

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    queue = aioprocessing.AioQueue()
    lock = aioprocessing.AioLock()
    event = aioprocessing.AioEvent()
    tasks = [
        asyncio.ensure_future(example(queue, event, lock)), 
        asyncio.ensure_future(example2(queue, event, lock)),
    ]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()

The aioprocessing objects can be used just like their multiprocessing equivalents - as they are in func above - but they can also be seamlessly used inside of asyncio coroutines, without ever blocking the event loop.

How does it work?

In most cases, this library makes blocking calls to multiprocessing methods asynchronous by executing the call in a ThreadPoolExecutor, using asyncio.run_in_executor(). It does not re-implement multiprocessing using asynchronous I/O. This means there is extra overhead added when you use aioprocessing objects instead of multiprocessing objects, because each one is generally introducing a ThreadPoolExecutor containing at least one threading.Thread. It also means that all the normal risks you get when you mix threads with fork apply here, too (See http://bugs.python.org/issue6721 for more info).

The one exception to this is aioprocessing.AioPool, which makes use of the existing callback and error_callback keyword arguments in the various Pool.*_async methods to run them as asyncio coroutines. Note that multiprocessing.Pool is actually using threads internally, so the thread/fork mixing caveat still applies.

Each multiprocessing class is replaced by an equivalent aioprocessing class, distinguished by the Aio prefix. So, Pool becomes AioPool, etc. All methods that 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).

Note that you can also use the aioprocessing synchronization primitives as replacements for their equivalent threading primitives, in single-process, multi-threaded programs that use asyncio.

What parts of multiprocessing are supported?

Most of them! All methods that could do blocking I/O in the following objects have equivalent versions in aioprocessing that extend the multiprocessing versions by adding coroutine versions of all the blocking methods.

  • Pool
  • Process
  • Pipe
  • Lock
  • RLock
  • Semaphore
  • BoundedSemaphore
  • Event
  • Condition
  • Barrier
  • connection.Connection
  • connection.Listener
  • connection.Client
  • Queue
  • JoinableQueue
  • SimpleQueue
  • All managers.SyncManager Proxy versions of the items above (SyncManager.Queue, SyncManager.Lock(), etc.).

What versions of Python are compatible?

aioprocessing will work out of the box on Python 3.5+.

Gotchas

Keep in mind that, while the API exposes coroutines for interacting with multiprocessing APIs, internally they are almost always being delegated to a ThreadPoolExecutor, this means the caveats that apply with using ThreadPoolExecutor with asyncio apply: namely, you won't be able to cancel any of the coroutines, because the work being done in the worker thread can't be interrupted.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].