【发布时间】:2019-11-22 06:58:21
【问题描述】:
当我创建一个子进程并通过标准输入和标准输出进行通信时,除非我刷新缓冲区或执行输入(),否则消息不会到达。
所以我想知道 input() 是否刷新缓冲区,如果是,我想知道为什么。
# file1
import subprocess
import time
import select
process = subprocess.Popen(['python3', 'file2.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
read_ready, _, _ = select.select([process.stdout], [], [])
message = read_ready[0].readline().decode()
print(message)
time.sleep(11)
process.kill()
-
# file2
import sys
import time
print('1')
message = input()
# I added the sleep because the buffer gets flushed if the program stops
time.sleep(10)
如果我执行此代码,它会立即打印 1。如果我用 input() 注释掉该行,那么我需要等到文件关闭
【问题讨论】:
标签: python-3.x input flush