gpt4 book ai didi

python subprocess32 超时,溢出错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:26 25 4
gpt4 key购买 nike

这是我的日志

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

所以触发这个的代码是

output = proc.communicate(timeout = timeout)[0]

超时设置为20,这种情况会间歇性地发生(几乎不会发生,但它会发生),我使用带有subprocess32库的python 2.7.11,这是一个python错误吗?

好的,我检查了 subprocess32.py,该行是这样的

endtime = time.time() + timeout

ready = poller.poll(self._remaining_time(endtime))

所以基本上时间戳太大而无法转换为 c int,我能做些什么来解决这个问题吗?

最佳答案

听起来像是一个错误。

如果您感兴趣,这里有一个解决方法建议:不要使用communicate,而是从线程中的进程stdout读取内容,并检查进程是否结束,仅通过以下方式:通过poll读取或返回代码yield。

由于您控制循环,因此您可以在主线程中等待 1 秒并倒计时超时(不是特别准确,因为 sleep 可能会漂移,但这足够好且简单)。当达到 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()

关于python subprocess32 超时,溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41555674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com