异步多进程通信管道
aiopipe的Python项目详细描述
aiopipe—用于异步的多进程通信管道
这个包包装^{
示例
下面的示例打开一个管道,子进程中的write端和read端 在父进程中结束:
fromcontextlibimportclosingfrommultiprocessingimportProcessimportasynciofromaiopipeimportaiopipeasyncdefmainTask(eventLoop):rx,tx=aiopipe()withtx.send()astx:proc=Process(target=childProc,args=(tx,))proc.start()# The write end is now available in the child process# and invalidated in the parent process.stream=awaitrx.open(eventLoop)msg=awaitstream.readline()assertmsg==b"hi from child process\n"proc.join()defchildProc(tx):eventLoop=asyncio.new_event_loop()stream=eventLoop.run_until_complete(tx.open(eventLoop))withclosing(stream):stream.write(b"hi from child process\n")eventLoop=asyncio.get_event_loop()eventLoop.run_until_complete(mainTask(eventLoop))
安装
此软件包要求python>;=3.5.0,可以使用pip
:
pip install aiopipe