Add a callback to execWithRedirect
If the callback returns true send the process a SIGTERM to terminate it.
This commit is contained in:
parent
dfe8774616
commit
f1d8b10867
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@ -66,7 +67,7 @@ class tee(threading.Thread):
|
|||||||
|
|
||||||
def execWithRedirect(command, argv, stdin = None, stdout = None,
|
def execWithRedirect(command, argv, stdin = None, stdout = None,
|
||||||
stderr = None, root = None, preexec_fn=None, cwd=None,
|
stderr = None, root = None, preexec_fn=None, cwd=None,
|
||||||
raise_err=False):
|
raise_err=False, callback_func=None, callback_args=None):
|
||||||
""" Run an external program and redirect the output to a file.
|
""" Run an external program and redirect the output to a file.
|
||||||
@param command The command to run.
|
@param command The command to run.
|
||||||
@param argv A list of arguments.
|
@param argv A list of arguments.
|
||||||
@ -143,7 +144,14 @@ def execWithRedirect(command, argv, stdin = None, stdout = None,
|
|||||||
preexec_fn=preexec_fn, cwd=cwd,
|
preexec_fn=preexec_fn, cwd=cwd,
|
||||||
env=env)
|
env=env)
|
||||||
|
|
||||||
proc.wait()
|
# Wait for the process to finish, calling callback_func to test for early termination
|
||||||
|
while proc.returncode is None:
|
||||||
|
time.sleep(5)
|
||||||
|
if callback_func and callback_func():
|
||||||
|
proc.terminate()
|
||||||
|
callback_func = None
|
||||||
|
proc.poll()
|
||||||
|
|
||||||
ret = proc.returncode
|
ret = proc.returncode
|
||||||
|
|
||||||
#close the input ends of pipes so we get EOF in the tee processes
|
#close the input ends of pipes so we get EOF in the tee processes
|
||||||
|
Loading…
Reference in New Issue
Block a user