我用了这两个问题的密码
how to get the return value from a thread in python
得到了这个
import subprocess, threading, os
class ThreadWithReturnValue(threading.Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None):
threading.Thread.__init__(self, group, target, name, args, kwargs, Verbose)
self._return = None
def run(self):
if self._Thread__target is not None:
self._return = self._Thread__target(*self._Thread__args, **self._Thread__kwargs)
def join(self, timeout = None):
threading.Thread.join(self, timeout)
return self._return
class SubprocessWrapper(object):
def __init__(self, cmd, timeout):
self.cmd = cmd
self.process = None
self.timeout = timeout
def run(self):
def target(cmd):
self.process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
returnValue = self.process.communicate()
return [returnValue[0], returnValue[1], self.process.returncode]
thread = ThreadWithReturnValue(target=target, args=[self.cmd])
thread.start()
returnValue = thread.join(self.timeout)
if thread.is_alive():
print 'cmd = ',self.cmd
self.process.kill()
returnValue = thread.join()
print 'rc = ', returnValue[2]
output = returnValue[0]
error = returnValue[1]
rc = returnValue[2]
return (output, rc)
os.system('date +%T.%N')
s1 = SubprocessWrapper("echo 'Process started'; sleep 2; echo 'Process finished'", timeout = 3)
s1.run()
os.system('date +%T.%N')
s2 = SubprocessWrapper("echo 'Process started'; sleep 2; echo 'Process finished'", timeout = 1)
s2.run()
os.system('date +%T.%N')
问题是输出是
^{pr2}$所以你可以看到,原本应该在一秒钟后终止的进程实际上花了2秒。这是因为join()
的缘故,但在问题subprocess with timeout
中,这很好。这意味着,当我整合了这两个代码时,我的问题是如何解决它?我在想我可能需要以不同的方式调用threading.Thread.__init__
方法,但我不明白如何调用。在
尽管超时,此代码不会在一秒钟内返回输出。它在睡眠后两秒钟内返回:
输出
^{pr2}$来自"Stop reading process output in Python without hang?" question的基于报警的解决方案:
输出
相关问题 更多 >
编程相关推荐