Python异步IO简单示例
我正在试验python的异步IO协议。我从官方文件中找到了它,想稍微修改一下,复制一下它的beahviour。因此,我编写了以下两个脚本:Python异步IO简单示例,python,multiprocessing,python-asyncio,python-3.5,event-driven,Python,Multiprocessing,Python Asyncio,Python 3.5,Event Driven,我正在试验python的异步IO协议。我从官方文件中找到了它,想稍微修改一下,复制一下它的beahviour。因此,我编写了以下两个脚本: # file: get_rand.py from random import choice from time import sleep def main(): print(choice('abcdefghijklmnopqrstuvwxyz')) sleep(2) if __name__ == '__main__': main(
# file: get_rand.py
from random import choice
from time import sleep
def main():
print(choice('abcdefghijklmnopqrstuvwxyz'))
sleep(2)
if __name__ == '__main__':
main()
以及:
当我运行async_test.py时,我得到以下输出:
$ python3.5 async_test.py
Event loop started
Process created
Getting pipes...
Protocol initialised
Waiting for child to exit...
Data received
它就这么挂着
如果我取消注释pipe\u connection\u lost
方法,则输出如下:
$ python3.5 async_test.py
Event loop started
Process created
Getting pipes...
Protocol initialised
Waiting for child to exit...
Data received
Pipe connection lost for the following reason:
None
但这个过程仍然悬而未决。我认为正在发生的是,由于某种原因,子进程(
get\u rand.py
)关闭管道(如上面的输出所示),但不会终止,以便父进程可以从退出退出退出退出。考虑到我的代码大多是从python文档中的一个示例复制粘贴的,我真的不理解这种行为背后的原因。Changedef subprocess\u exited(self):
到
$ python3.5 async_test.py
Event loop started
Process created
Getting pipes...
Protocol initialised
Waiting for child to exit...
Data received
Pipe connection lost for the following reason:
None