【发布时间】:2021-11-21 01:52:00
【问题描述】:
我有 2 个进程。我希望write.py 通过 Linux 管道向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)
如给定的,当我运行 python write.py 时,我得到了预期的输出:
Got something!
hello
但是当我取消注释有问题的代码时,我没有得到任何输出。为什么?
- Python 3.9.7
- Linux 5.4.72-microsoft-standard-WSL2 x86_64
【问题讨论】:
标签: python subprocess python-asyncio