[英]Why does starting an asyncio event loop in another thread mess with my pipe?
I have 2 processes.我有 2 个进程。 I would like write.py
to write a message to read.py
through a Linux pipe.我想write.py
通过 Linux pipe 向read.py
写一条消息。
### write.py
import subprocess
import asyncio
from threading import Thread
# !-- Offending code here. --!
# loop = asyncio.new_event_loop()
# def side_thread(loop):
# asyncio.set_event_loop(loop)
# loop.run_forever()
# thread = Thread(target=side_thread, args=(loop,))
# thread.start()
notify_proc = subprocess.Popen("python read.py".split(), stdin=subprocess.PIPE)
notify_proc.stdin.write("hello\n".encode("utf-8"))
### read.py
import sys
for line in sys.stdin:
print("Got something!")
print(line)
As given, when I run python write.py
, I get the expected output:如给定的,当我运行python write.py
时,我得到了预期的 output:
Got something!
hello
But when I uncomment the offending code, I don't get any output.但是当我取消注释有问题的代码时,我没有得到任何 output。 Why?为什么?
Your buffer won't get sent over until the buffer is flushed or the process ends, and the process will not end as long as a non-daemon thread is running.在刷新缓冲区或进程结束之前,您的缓冲区不会被发送过来,并且只要非守护线程正在运行,进程就不会结束。 In the short term, you can add notify_proc.stdin.flush()
, but your mixing of multiprocessing, threading and asyncio is going to lead to tears.在短期内,您可以添加notify_proc.stdin.flush()
,但是您将多处理、线程和异步混合使用会导致流泪。 What on earth are you trying to do here?你到底想在这里做什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.