def runner(argv, timeout=0):
"""
Run the input command-line executable (specified in a Popen-style list) and
return its exit code. Optionally specify a timeout. If timeout is 0 or
None, simply wait until the process is done.
"""
def stringify(xs):
return map(str, xs)
argv = list(stringify(argv))
proc = subprocess.Popen(argv,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=False)
t0 = time.time()
while True:
exit_code = proc.poll()
if exit_code is None:
# Process is still running
if timeout > 0 and time.time() - t0 >= timeout:
proc.kill()
stdout = proc.stdout.read()
stderr = proc.stderr.read()
raise subprocess.TimeoutExpired(cmd=' '.join(argv),
timeout=timeout,
output=stdout,
stderr=stderr)
else:
return proc.returncode
# time.sleep(1) <-- BAD idea
await asyncio.sleep(.1)
评论列表
文章目录