提交 7cae8d18 编写于 作者: L Lukáš Doktor

utils.process: Support timeout for "wait"

Our process library only allows to specify timeout for process execution
when used as the whole package using "run" method, but it might be
useful to allow the same kind of treatement for "wait". Let's move the
code there.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 77b3e7bd
......@@ -808,14 +808,47 @@ class SubProcess(object):
self._fill_results(rc)
return rc
def wait(self):
def wait(self, timeout=None, sig=signal.SIGTERM):
"""
Call the subprocess poll() method, fill results if rc is not None.
:param timeout: Time (seconds) we'll wait until the process is
finished. If it's not, we'll try to terminate it
and get a status.
:param sig: Signal to send to the process in case it did not end after
the specified timeout.
"""
def timeout_handler():
self.send_signal(sig)
self.result.interrupted = "timeout after %ss" % timeout
self._init_subprocess()
rc = self._popen.wait()
if rc is not None:
self._fill_results(rc)
rc = None
if timeout is None:
rc = self._popen.wait()
elif timeout > 0.0:
timer = threading.Timer(timeout, timeout_handler)
try:
timer.start()
rc = self._popen.wait()
finally:
timer.cancel()
if rc is None:
stop_time = time.time() + 1
while time.time() < stop_time:
rc = self._popen.wait()
if rc is not None:
break
else:
self.kill()
rc = self._popen.wait()
if rc is None:
# If all this work fails, we're dealing with a zombie process.
raise AssertionError('Zombie Process %s' % self._popen.pid)
self._fill_results(rc)
return rc
def stop(self):
......@@ -868,36 +901,8 @@ class SubProcess(object):
:returns: The command result object.
:rtype: A :class:`CmdResult` instance.
"""
def timeout_handler():
self.send_signal(sig)
self.result.interrupted = "timeout after %ss" % timeout
self._init_subprocess()
if timeout is None:
self.wait()
elif timeout > 0.0:
timer = threading.Timer(timeout, timeout_handler)
try:
timer.start()
self.wait()
finally:
timer.cancel()
if self.result.exit_status is None:
stop_time = time.time() + 1
while time.time() < stop_time:
self.poll()
if self.result.exit_status is not None:
break
else:
self.kill()
self.poll()
# If all this work fails, we're dealing with a zombie process.
e_msg = 'Zombie Process %s' % self._popen.pid
assert self.result.exit_status is not None, e_msg
self.wait(timeout, sig)
return self.result
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册