【发布时间】:2015-02-12 11:18:27
【问题描述】:
使用以下代码,poll_obj.poll() 会阻塞直到睡眠完成:
import select, subprocess, time, fcntl, os
poll_obj = select.poll()
popen_obj = subprocess.Popen("sleep 5", stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, shell=True)
fcntl.fcntl(popen_obj.stdout, fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(popen_obj.stderr, fcntl.F_SETFL, os.O_NONBLOCK)
poll_obj.register(popen_obj.stdout)
poll_obj.register(popen_obj.stderr)
start_time = time.time()
poll_obj.poll()
print(time.time() - start_time)
据我了解,poll_obj.poll() 不应阻塞,因为在它跟踪的 FD 上设置了 O_NONBLOCK 标志。它应该返回None。
有没有办法防止poll_obj.poll()被屏蔽?
【问题讨论】:
标签: python subprocess