python Popen hangs



我使用Popen打开tclsh,并尝试运行puts aaaaaaaaaaa...命令(aaa…

通过PIPE发送命令

它工作得很好,但是,当我运行命令两次(aaaa…需要足够长的时间),并且我通过PIPE读取stdout,代码将挂起

我试图扩大默认字体大小,但它不工作

钩子下面发生了什么?有人能帮忙吗?

proc = Popen(
['C:\Tcl\bin\tclsh.exe'],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE
)
proc.stdin.write(bytearray('puts ' + 'a' * 100000 + 'endsn', 'utf-8'))
proc.stdin.write(bytearray('puts ' + 'a' * 100000 + 'endsn', 'utf-8'))
proc.stdin.flush()
stdout = b''
while proc.poll() == None:
if stdout.endswith(b'ends'):
break
else:
stdout += proc.stdout.read1(1)
print(stdout.decode('utf-8'))

最后,我在stdout中使用tempfile代替PIPE

self._tempfile = tempfile.mktemp()
self._tempfile_in = open(self._tempfile, 'wb')
self._tempfile_out = open(self._tempfile, 'rb')
self._process = subprocess.Popen(
[self.tcl_exe] + list(self.tcl_exe_args),
stdin = subprocess.PIPE,
stdout = self._tempfile_in,
stderr = subprocess.PIPE)

……

self._tempfile_out.read1(1)

管道缓冲区不是无限大的。您填充了标准输入缓冲区,然后tclsh填充了它的标准输出缓冲区,并且它不能向前移动以读取下一行,直到您从标准输出中取出一些东西。因此,您在第二次写入时被阻塞了。

最新更新