一个python 3.3+库,它将多处理模块与asyncio集成在一起。
aioprocessing的Python项目详细描述
人工智能处理[构建状态](https://travis-ci.org/dano/aioprocessing.svg?branch=master)"(https://travis ci.org/dano/aioprocessing)
`aioprocessing`提供异步,[`asyncio`](https://docs.python.org/3/library/asyncio.html)兼容,coroutine
[`multiprocessing`]中对象上的许多阻塞实例方法的版本(https://docs.python.org/3/library/multiprocessing.html)
库。下面是一个示例,演示
"event"、"queue"和"lock"的"aioprocessing"版本:
``python
导入时间
导入异步
导入aioprocessing
导入多处理
def func(queue、event、lock,项目):
"demo worker函数。
精确地
使用原始多处理对象的方式。
"
带锁:
event.set()
对于项中的项:
time.sleep(3)
queue.put(item+5)
queue.close()
@asyncio.coroutine
def example(queue,event,lock):
l=[1,2,3,4,5]
p=aioprocessing.aioprocess(target=func,args=(queue,event,lock,l))
p.start()
为true时:
result=yield from queue.coro_get()
无:
break
print("got result{}.format(result))
p.coro_join()
@asyncio.coroutine
def example2(queue,event,锁):
从event.coro_wait()中产生收益
(从锁中产生收益):
从queue.coro_put中产生收益(78)
从queue.coro_put(none)中产生收益
queue=aioprocessing.aioqueue()
lock=aioprocessing.aiolock()
event=aioprocessing.aioevent()
tasks=[
异步。确保未来(例如(队列、事件、锁)),
异步。确保未来(例如2(队列、事件、锁)),
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
````
``python
async def example2(queue,event,lock):
await event.coro_wait()
async with lock:
await queue.coro_put(78)
await queue.coro_put(none)关闭worker
``
aioprocessing对象可以像它们的多处理等价物一样使用,就像它们在上面的"func"中一样,但是它们也可以在"asyncio"协程中无缝地使用,而不会阻塞事件循环。
----
在大多数情况下,此库通过在一个[`threadpoolexecutor`]中执行调用(https://docs.python.org/3/library/concurrent.futures.html threadpoolexecutor),对"多处理"方法进行阻塞调用。使用
[`asyncio.run戋in戋executor()`](https://docs.python.org/3/library/asyncio eventloop.html戋asyncio.baseeventloop.run戋in戋executor)。
它不会*使用异步I/O重新实现多处理。这意味着
使用"aioprocessing"对象而不是
"multiprocessing"对象时会增加额外的开销,因为每个线程通常都会引入一个
`threadpoolexecutor',其中至少包含一个[`threading.thread`](https://docs.python.org/2/library/threading.html thread objects)。这也意味着
当您将线程与fork混合时,您所面临的所有正常风险也适用于这里
(有关更多信息,请参见http://bugs.python.org/issue6721)。
`,它使用各种
[`pool.*u async`]方法(https://docs.python.org/3/library/multiprocessing.html multiprocessing.pool.pool.apply嫒async)中的
现有的"callback"和"error嫒callback"关键字参数将它们作为"asyncio"协程运行。请注意,
"multiprocessing.pool"实际上在内部使用线程,因此thread/fork
混合警告仍然适用。
每个"multiprocessing"类都替换为等效的"aioprocessing"类,
以"aio"前缀区分。因此,"pool"变为"aiopool"等。所有可以阻塞I/O的方法也都有一个可以与"asyncio"一起使用的协程版本。例如,"multiprocessing.lock.acquire()"可以替换为"aioproprocessing.aiolock.coro_acquire()"。可以使用"loop"关键字参数将"asyncio"eventloop对象传递给任何"coro"方法。例如,"lock.coro_acquire(loop=my_loop)"。
使用"异步"的多线程程序
。
支持多处理的哪些部分?
——可以在以下对象中阻塞I/O的所有方法在"aioprocessing"中都有相同的版本,通过添加所有阻塞方法的协程版本来扩展"多处理"版本。
-`pool`
-`process`
-`pipe`
-`lock`
-`rlock`
-`信号量`
-`boundedsemaphore`
-`event`
-`condition`
-`barrier`
-`connection.connection`
-`connection.listener`
-`connection.client`
-`queue`
-`joinablequeue`
-`simplequeue`
-`all`managers.syncmanager``proxy`上述项目的版本(`syncmanager.queue`,`syncmanager.lock()`,等等)。
---------------------------
`aioprocessing`将在python 3.4+上开箱即用。
`aioprocessing`提供异步,[`asyncio`](https://docs.python.org/3/library/asyncio.html)兼容,coroutine
[`multiprocessing`]中对象上的许多阻塞实例方法的版本(https://docs.python.org/3/library/multiprocessing.html)
库。下面是一个示例,演示
"event"、"queue"和"lock"的"aioprocessing"版本:
``python
导入时间
导入异步
导入aioprocessing
导入多处理
def func(queue、event、lock,项目):
"demo worker函数。
精确地
使用原始多处理对象的方式。
"
带锁:
event.set()
对于项中的项:
time.sleep(3)
queue.put(item+5)
queue.close()
@asyncio.coroutine
def example(queue,event,lock):
l=[1,2,3,4,5]
p=aioprocessing.aioprocess(target=func,args=(queue,event,lock,l))
p.start()
为true时:
result=yield from queue.coro_get()
无:
break
print("got result{}.format(result))
p.coro_join()
@asyncio.coroutine
def example2(queue,event,锁):
从event.coro_wait()中产生收益
(从锁中产生收益):
从queue.coro_put中产生收益(78)
从queue.coro_put(none)中产生收益
queue=aioprocessing.aioqueue()
lock=aioprocessing.aiolock()
event=aioprocessing.aioevent()
tasks=[
异步。确保未来(例如(队列、事件、锁)),
异步。确保未来(例如2(队列、事件、锁)),
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
````
``python
async def example2(queue,event,lock):
await event.coro_wait()
async with lock:
await queue.coro_put(78)
await queue.coro_put(none)关闭worker
``
aioprocessing对象可以像它们的多处理等价物一样使用,就像它们在上面的"func"中一样,但是它们也可以在"asyncio"协程中无缝地使用,而不会阻塞事件循环。
----
在大多数情况下,此库通过在一个[`threadpoolexecutor`]中执行调用(https://docs.python.org/3/library/concurrent.futures.html threadpoolexecutor),对"多处理"方法进行阻塞调用。使用
[`asyncio.run戋in戋executor()`](https://docs.python.org/3/library/asyncio eventloop.html戋asyncio.baseeventloop.run戋in戋executor)。
它不会*使用异步I/O重新实现多处理。这意味着
使用"aioprocessing"对象而不是
"multiprocessing"对象时会增加额外的开销,因为每个线程通常都会引入一个
`threadpoolexecutor',其中至少包含一个[`threading.thread`](https://docs.python.org/2/library/threading.html thread objects)。这也意味着
当您将线程与fork混合时,您所面临的所有正常风险也适用于这里
(有关更多信息,请参见http://bugs.python.org/issue6721)。
`,它使用各种
[`pool.*u async`]方法(https://docs.python.org/3/library/multiprocessing.html multiprocessing.pool.pool.apply嫒async)中的
现有的"callback"和"error嫒callback"关键字参数将它们作为"asyncio"协程运行。请注意,
"multiprocessing.pool"实际上在内部使用线程,因此thread/fork
混合警告仍然适用。
每个"multiprocessing"类都替换为等效的"aioprocessing"类,
以"aio"前缀区分。因此,"pool"变为"aiopool"等。所有可以阻塞I/O的方法也都有一个可以与"asyncio"一起使用的协程版本。例如,"multiprocessing.lock.acquire()"可以替换为"aioproprocessing.aiolock.coro_acquire()"。可以使用"loop"关键字参数将"asyncio"eventloop对象传递给任何"coro"方法。例如,"lock.coro_acquire(loop=my_loop)"。
使用"异步"的多线程程序
。
支持多处理的哪些部分?
——可以在以下对象中阻塞I/O的所有方法在"aioprocessing"中都有相同的版本,通过添加所有阻塞方法的协程版本来扩展"多处理"版本。
-`pool`
-`process`
-`pipe`
-`lock`
-`rlock`
-`信号量`
-`boundedsemaphore`
-`event`
-`condition`
-`barrier`
-`connection.connection`
-`connection.listener`
-`connection.client`
-`queue`
-`joinablequeue`
-`simplequeue`
-`all`managers.syncmanager``proxy`上述项目的版本(`syncmanager.queue`,`syncmanager.lock()`,等等)。
`aioprocessing`将在python 3.4+上开箱即用。