subprocess应用验证与建立模板

位置:F:/pyqt_practice/test_one_shot_training_multi_region_py/code/testprocess.py
注:usetotest.py 与testprocess.py 文件的相对位置是,两者处于同一个文件夹中。

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

  1. caseone:代码位置: 前循后主
    for 循环没有p.wait()+后半段主程序中没有wait(),所有的child process没没有执行完成
  2. casetwo: 代码位置:前循后主p
    for 循环没有p.wait()+后半段主程序中有wait(),所有的child process 都执行完成 first:for 循环child process 无序完成+second 主程序后半段child process 完成
  3. 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 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值