subprocess.wait(超时=15) 不工作
subprocess.wait(timeout=15) is not working
代码:
import os
import subprocess
execs = ['C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof2.exe', # --> Child 1
'C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof5.exe', # --> Child 2
'C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof10.exe',...] # --> Child 3 and more
print('Parent Process id : ', os.getpid())
process = [subprocess.Popen(exe) for exe in execs]
for proc in process:
try:
proc.wait(timeout=15)
print('Child Process id : ', proc.pid)
if proc.returncode == 0:
print(proc.pid, 'Exited')
except subprocess.TimeoutExpired:
proc.terminate()
print('Child Process with pid',proc.pid ,'is killed')
某些子进程的执行时间将超过 15 秒。所以,我必须杀死超时的进程。但是 proc.wait(timeout=15)
并没有引发异常,而是执行了该过程。
我也试过 [subprocess.Popen(exe, timeout=15) for exe in execs]
但得到了
错误:
TypeError: __init__() got an unexpected keyword argument 'timeout'
您不是在并行等待进程 - 您是给每个进程连续的 15 秒时间来完成他们的工作。
您可能需要这样的东西,让所有这些并行时间最多为 15 秒。
import os
import subprocess
import time
execs = [
"C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof2.exe",
"C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof5.exe",
"C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof10.exe",
]
print("Parent Process id : ", os.getpid())
process = [subprocess.Popen(exe) for exe in execs]
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
any_alive = False
for proc in process:
ret = proc.poll()
if ret is None: # still alive
if elapsed_time >= 15:
print("Killing:", proc.pid)
proc.terminate()
any_alive = True
if not any_alive:
break
time.sleep(1)
代码:
import os
import subprocess
execs = ['C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof2.exe', # --> Child 1
'C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof5.exe', # --> Child 2
'C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof10.exe',...] # --> Child 3 and more
print('Parent Process id : ', os.getpid())
process = [subprocess.Popen(exe) for exe in execs]
for proc in process:
try:
proc.wait(timeout=15)
print('Child Process id : ', proc.pid)
if proc.returncode == 0:
print(proc.pid, 'Exited')
except subprocess.TimeoutExpired:
proc.terminate()
print('Child Process with pid',proc.pid ,'is killed')
某些子进程的执行时间将超过 15 秒。所以,我必须杀死超时的进程。但是 proc.wait(timeout=15)
并没有引发异常,而是执行了该过程。
我也试过 [subprocess.Popen(exe, timeout=15) for exe in execs]
但得到了
错误:
TypeError: __init__() got an unexpected keyword argument 'timeout'
您不是在并行等待进程 - 您是给每个进程连续的 15 秒时间来完成他们的工作。
您可能需要这样的东西,让所有这些并行时间最多为 15 秒。
import os
import subprocess
import time
execs = [
"C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof2.exe",
"C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof5.exe",
"C:\Users\XYZ\PycharmProjects\Task1\dist\Multiof10.exe",
]
print("Parent Process id : ", os.getpid())
process = [subprocess.Popen(exe) for exe in execs]
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
any_alive = False
for proc in process:
ret = proc.poll()
if ret is None: # still alive
if elapsed_time >= 15:
print("Killing:", proc.pid)
proc.terminate()
any_alive = True
if not any_alive:
break
time.sleep(1)