位置:F:/pyqt_practice/test_one_shot_training_multi_region_py/code/testprocess.py
注:usetotest.py 与testprocess.py 文件的相对位置是,两者处于同一个文件夹中。
subprocess应用验证与建立模板
usetotest.py
import argparse
import datetime
parser = argparse.ArgumentParser(formatter_class = argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--your_name',
default = 'datou',
help='please input your name')
args = parser.parse_args()
print("your name is :",args.your_name)
print("{} is end!".format(args.your_name))
print("{} the end time is :{}".format(args.your_name,datetime.datetime.now()))
用testprocess.py去进行验证
caseone
import subprocess
import shlex
name_list = ['one','two','three','four','five','six']
for name in name_list:
print("starting to process the ",name)
command = 'python usetotest.py --your_name={}'.format(name)
p = subprocess.Popen(command)
print("surface~~")
print("surface--")
print()
# p.wait()
command2 = 'python usetotest.py --your_name=haha'
p2 = subprocess.Popen(command2)
#p2.wait()
print("whether p2 has teminated?:",p2.poll())
print("the main program ends")
输出
casetwo
import subprocess
import shlex
name_list = ['one','two','three','four','five','six']
for name in name_list:
print("starting to process the ",name)
command = 'python usetotest.py --your_name={}'.format(name)
p = subprocess.Popen(command)
print("surface~~")
print("surface--")
print()
# p.wait()
command2 = 'python usetotest.py --your_name=haha'
p2 = subprocess.Popen(command2)
p2.wait()
print("whether p2 has teminated?:",p2.poll())
print("the main program ends")
casetwo输出
casethree
import subprocess
import shlex
command2 = 'python usetotest.py --your_name=haha'
p2 = subprocess.Popen(command2)
p2.wait()
print("whether p2 has teminated?:",p2.poll())
name_list = ['one','two','three','four','five','six']
for name in name_list:
print("starting to process the ",name)
command = 'python usetotest.py --your_name={}'.format(name)
p = subprocess.Popen(command)
print("surface~~")
print("surface--")
print()
# p.wait()
print("the main program ends")
casethree输出
casefour
import subprocess
import shlex
command2 = 'python usetotest.py --your_name=haha'
p2 = subprocess.Popen(command2)
p2.wait()
print("whether p2 has teminated?:",p2.poll())
name_list = ['one','two','three','four','five','six']
for name in name_list:
print("starting to process the ",name)
command = 'python usetotest.py --your_name={}'.format(name)
p = subprocess.Popen(command)
print("surface~~")
print("surface--")
print()
p.wait()
print("the main program ends")
输出
casefive
import subprocess
import shlex
name_list = ['one','two','three','four','five','six']
for name in name_list:
print("starting to process the ",name)
command = 'python usetotest.py --your_name={}'.format(name)
p = subprocess.Popen(command)
print("surface~~")
print("surface--")
p.wait()
print()
command2 = 'python usetotest.py --your_name=haha'
p2 = subprocess.Popen(command2)
p2.wait()
print("whether p2 has teminated?:",p2.poll())
print("the main program ends")
输出
综上所述:
p = subprocess.Popen(arg) arg可以是一个字符串去代表命令:caseone casetwo casethree casefout
也可以是一个字符串列表 case five
p2.poll() 若执行完成返回0, 没有执行完成,返回None
- caseone:代码位置: 前循后主
for 循环没有p.wait()+后半段主程序中没有wait(),所有的child process没没有执行完成 - casetwo: 代码位置:前循后主p
for 循环没有p.wait()+后半段主程序中有wait(),所有的child process 都执行完成 first:for 循环child process 无序完成+second 主程序后半段child process 完成 - casethree:代码位置:前主p后循
for循环在主程序后半段+没有p.wait() ,for循环执行阶段 其child process 没有执行完成,主程序时执行部分结束。 前半段主程序+p.wait 其child process执行完成
4.casefour:代码位置: 前主p后循p child process按代码循序完成
5.casefive: 代码位置:前循p后主p child process按代码顺序完成
所以运用subprocess 去执行程序的话,可以使用casefour 或者是casefive 作为参考的框架,保证程序按代码顺序执行
如果不想按照代码位置顺序执行,可以参考casetwo 。