一旦您了解了 *unix 中的全流程运行机制,您将很容易找到更简单的解决方案:
考虑这个简单的例子,如何使用 select.select() (现在几乎在 *nix 上几乎所有地方都可用)来制作可超时的communicate() 方法。这也可以用 epoll/poll/kqueue 编写,但是 select.select() 变体可能是一个很好的例子。select.select() 的主要限制(速度和 1024 max fds)不适用于您的任务。
这在 *nix 下工作,不创建线程,不使用信号,可以从任何线程(不仅是主线程)启动,并且足够快以从我的机器(i5 2.3ghz)上的标准输出读取 250mb/s 的数据。
在通信结束时加入 stdout/stderr 时出现问题。如果您有大量程序输出,这可能会导致大量内存使用。但是你可以用较小的超时时间多次调用communicate()。
class Popen(subprocess.Popen):
def communicate(self, input=None, timeout=None):
if timeout is None:
return subprocess.Popen.communicate(self, input)
if self.stdin:
# Flush stdio buffer, this might block if user
# has been writing to .stdin in an uncontrolled
# fashion.
self.stdin.flush()
if not input:
self.stdin.close()
read_set, write_set = [], []
stdout = stderr = None
if self.stdin and input:
write_set.append(self.stdin)
if self.stdout:
read_set.append(self.stdout)
stdout = []
if self.stderr:
read_set.append(self.stderr)
stderr = []
input_offset = 0
deadline = time.time() + timeout
while read_set or write_set:
try:
rlist, wlist, xlist = select.select(read_set, write_set, [], max(0, deadline - time.time()))
except select.error as ex:
if ex.args[0] == errno.EINTR:
continue
raise
if not (rlist or wlist):
# Just break if timeout
# Since we do not close stdout/stderr/stdin, we can call
# communicate() several times reading data by smaller pieces.
break
if self.stdin in wlist:
chunk = input[input_offset:input_offset + subprocess._PIPE_BUF]
try:
bytes_written = os.write(self.stdin.fileno(), chunk)
except OSError as ex:
if ex.errno == errno.EPIPE:
self.stdin.close()
write_set.remove(self.stdin)
else:
raise
else:
input_offset += bytes_written
if input_offset >= len(input):
self.stdin.close()
write_set.remove(self.stdin)
# Read stdout / stderr by 1024 bytes
for fn, tgt in (
(self.stdout, stdout),
(self.stderr, stderr),
):
if fn in rlist:
data = os.read(fn.fileno(), 1024)
if data == '':
fn.close()
read_set.remove(fn)
tgt.append(data)
if stdout is not None:
stdout = ''.join(stdout)
if stderr is not None:
stderr = ''.join(stderr)
return (stdout, stderr)