从存储的 subprocess.Popen 对象获取提供给 subprocess.Popen 的输入
Getting input given to subprocess.Popen from stored subprocess.Popen object
如果我有一个 subprocess.Popen
个对象的列表,有什么方法可以告诉我在生成它们时最初使用的命令是什么?
Python 2.7
上下文:
我有一个启动测试的各种命令的列表。如果其中一项测试失败,脚本会清理环境。然后我只想重试那些失败的命令。
注意:以下命令仅用于演示目的;在我的产品代码中调用的那些更复杂,但这不是重点。如果我能让它工作,它将与我的 prod cmds 一起工作。
commands = [['nosetests', '-V'],
['nosetests', '--collect-only'],
['nosetests', '--with-id']
]
def cleanup_env():
...do things...
def run_in_parallel(cmds, retry):
retry_tasks = []
if not cmds:
return
def done(p):
return p.poll() is not None
def success(p):
return p.returncode == 0
def fail(p):
if not retry:
retry_tasks.append(p)
print("{} failed, will need to retry.".format(retry_tasks))
else:
pass # if this is already a retry, we don't care, not going to retry again
MAX_PARALLEL = 4
processes = []
while True:
while cmds and len(processes) < MAX_PARALLEL:
task = cmds.pop() # pop last cmd off the stack
processes.append(subprocess.Popen(task))
for p in processes:
if done(p):
if success(p):
processes.remove(p)
else:
fail(p)
processes.remove(p)
if not processes and not cmds:
break
else:
time.sleep(0.05)
return retry_tasks
调用上面:
retry_list=run_in_parallel(commands, False)
if retry_list:
cleanup_env()
run_in_parallel(retry_list, True)
第一部分有效,但调用重试无效,因为我传递的是 subprocess.Popen
个对象的列表,而不是它们的初始输入。
因此问题来了,我如何获取 subprocess.Popoen
对象的输入?
Python2.7中没有Popen.args
。您可以为 processes
使用字典而不是列表,其中键是 Popen
实例,值是对应的 Popen
参数。
如果我有一个 subprocess.Popen
个对象的列表,有什么方法可以告诉我在生成它们时最初使用的命令是什么?
Python 2.7
上下文: 我有一个启动测试的各种命令的列表。如果其中一项测试失败,脚本会清理环境。然后我只想重试那些失败的命令。
注意:以下命令仅用于演示目的;在我的产品代码中调用的那些更复杂,但这不是重点。如果我能让它工作,它将与我的 prod cmds 一起工作。
commands = [['nosetests', '-V'],
['nosetests', '--collect-only'],
['nosetests', '--with-id']
]
def cleanup_env():
...do things...
def run_in_parallel(cmds, retry):
retry_tasks = []
if not cmds:
return
def done(p):
return p.poll() is not None
def success(p):
return p.returncode == 0
def fail(p):
if not retry:
retry_tasks.append(p)
print("{} failed, will need to retry.".format(retry_tasks))
else:
pass # if this is already a retry, we don't care, not going to retry again
MAX_PARALLEL = 4
processes = []
while True:
while cmds and len(processes) < MAX_PARALLEL:
task = cmds.pop() # pop last cmd off the stack
processes.append(subprocess.Popen(task))
for p in processes:
if done(p):
if success(p):
processes.remove(p)
else:
fail(p)
processes.remove(p)
if not processes and not cmds:
break
else:
time.sleep(0.05)
return retry_tasks
调用上面:
retry_list=run_in_parallel(commands, False)
if retry_list:
cleanup_env()
run_in_parallel(retry_list, True)
第一部分有效,但调用重试无效,因为我传递的是 subprocess.Popen
个对象的列表,而不是它们的初始输入。
因此问题来了,我如何获取 subprocess.Popoen
对象的输入?
Python2.7中没有Popen.args
。您可以为 processes
使用字典而不是列表,其中键是 Popen
实例,值是对应的 Popen
参数。