OS
This method has its disadvantage, if we want to stop this process. We need do it by ourselves except seize the terminal SIGNAL of this process by another shell.
import os
os.system('F:/My_Tool/evalvid-2.7-w32/psnr.exe 1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr.txt')
ShellExecute
ShellExecute(hwnd, op , file , params , dir , bShow )
其参数含义如下所示。
hwnd:父窗口的句柄,如果没有父窗口,则为0。
op:要进行的操作,为“open”、“print”或者为空。
file:要运行的程序,或者打开的脚本。
params:要向程序传递的参数,如果打开的为文件,则为空。
dir:程序初始化的目录。
bShow:是否显示窗口。
win32api.ShellExecute(0, 'open', 'F:/My_Tool/evalvid-2.7-w32/psnr.exe','1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv >F:/My_Tool/evalvid-2.7-w32/ref_psnr2.txt','',1)
This method has its disadvantage, this method just starts one process and do to next shell of python. But on the other hand, this may be the
better way to start a process without block.
CreateProcess
CreateProcess(appName, commandLine , processAttributes , threadAttributes , bInheritHandles ,dwCreationFlags , newEnvironment , currentDirectory , startupinfo )
其参数含义如下。
appName:可执行的文件名。
commandLine:命令行参数。
processAttributes:进程安全属性,如果为None,则为默认的安全属性。
threadAttributes:线程安全属性,如果为None,则为默认的安全属性。
bInheritHandles:继承标志。
dwCreationFlags:创建标志。
newEnvironment:创建进程的环境变量。
currentDirectory:进程的当前目录。
startupinfo :创建进程的属性。
This process is useless.
import win32api
import win32process
import win32event
import os
handle = win32process.CreateProcess('F:/My_Tool/evalvid-2.7-w32/psnr.exe','1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr2.txt', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO())
win32event.WaitForSingleObject(handle[0], -1) #win32process.TerminateProcess(handle[0],0)
Subprocess Popen
This method is the best method I have seen. Though it has block which I need as I should run the same exe for many times to do my batch processing.
import os
from subprocess import Popen,PIPE
cmda = 'F:/My_Tool/evalvid-2.7-w32/psnr.exe 1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr2.txt'
p2 = Popen(cmda, shell = True, stdout = PIPE, stderr = PIPE)
Reference:
http://blog.csdn.net/igolang/article/details/8524323
http://blog.csdn.net/menglei8625/article/details/7494094
http://blog.csdn.net/gray13/article/details/7044453