[英]python subprocess32 with timeout, overflowerror
this is my log这是我的日志
File "/opt/ibm/db2-governor/helpers/utils.py", line 10, in run_cmd
output = proc.communicate(timeout = timeout)[0]
File "/opt/ibm/dynamite/python/lib/python2.7/site-packages/subprocess32.py", line 927, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/opt/ibm/dynamite/python/lib/python2.7/site-packages/subprocess32.py", line 1713, in _communicate
orig_timeout)
File "/opt/ibm/dynamite/python/lib/python2.7/site-packages/subprocess32.py", line 1786, in _communicate_with_poll
ready = poller.poll(self._remaining_time(endtime))
OverflowError: Python int too large to convert to C lon
so the code that triggers this is所以触发这个的代码是
output = proc.communicate(timeout = timeout)[0]
timeout is set to 20, this happens intermitently (almost never but it happens), im using python 2.7.11 with subprocess32 library, is this a python bug?超时设置为 20,这会间歇性地发生(几乎从未发生过),我使用的是带有 subprocess32 库的 python 2.7.11,这是一个 python 错误吗?
ok, i checked subprocess32.py, the line goes like this好的,我检查了 subprocess32.py,该行是这样的
endtime = time.time() + timeout
ready = poller.poll(self._remaining_time(endtime))
so basically timestamp is too large to convert into ac int, is there anything i can do to resolve this?所以基本上时间戳太大而无法转换为 ac int,我可以做些什么来解决这个问题?
Sounds like a bug all right.听起来像一个错误好吧。
If you're interested, here's a workaround proposal: instead of communicate
, read from process stdout
in a thread and check if process is over by either nothing more to read or return code yield through poll
.如果你有兴趣,这里有一个解决办法的建议:与其
communicate
,从进程读取stdout
中的一个线索,并检查过程结束由要么没有更多的阅读或通过返回代码产量poll
。
Since you control the loop, you can wait 1 second in main thread and countdown for the timeout (not extra accurate, since sleep
can drift, but that would be good enough & simple).由于您控制循环,因此您可以在主线程中等待 1 秒并为超时倒计时(不是特别准确,因为
sleep
可能会漂移,但这已经足够简单了)。 Also kill the process when reaches 0.当达到 0 时也终止进程。
import threading
output = ""
def subp(p):
global output
while True:
# read blocks but since we're in a thread it doesn't matter
data = proc.stdout.read()
if not data or proc.poll() != None:
break
output += data
# here create the process
proc = subprocess...
# create a thread, pass the process handle
t = threading.Thread(target=subp,args=(proc,))
while True:
if proc.poll() != None:
# exit: OK
break
timeout -= 1
if timeout < 0:
# took too long: kill
proc.terminate()
break
time.sleep(1)
t.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.