python進(jìn)程類(lèi)subprocess的一些操作方法例子
subprocess.Popen用來(lái)創(chuàng)建子進(jìn)程。
1)Popen啟動(dòng)新的進(jìn)程與父進(jìn)程并行執(zhí)行,默認(rèn)父進(jìn)程不等待新進(jìn)程結(jié)束。
def TestPopen():
import subprocess
p=subprocess.Popen("dir",shell=True)
for i in range(250) :
print ("other things")
2)p.wait函數(shù)使得父進(jìn)程等待新創(chuàng)建的進(jìn)程運(yùn)行結(jié)束,然后再繼續(xù)父進(jìn)程的其他任務(wù)。且此時(shí)可以在p.returncode中得到新進(jìn)程的返回值。
def TestWait():
import subprocess
import datetime
print (datetime.datetime.now())
p=subprocess.Popen("sleep 10",shell=True)
p.wait()
print (p.returncode)
print (datetime.datetime.now())
3) p.poll函數(shù)可以用來(lái)檢測(cè)新創(chuàng)建的進(jìn)程是否結(jié)束。
def TestPoll():
import subprocess
import datetime
import time
print (datetime.datetime.now())
p=subprocess.Popen("sleep 10",shell=True)
t = 1
while(t <= 5):
time.sleep(1)
p.poll()
print (p.returncode)
t+=1
print (datetime.datetime.now())
4) p.kill或p.terminate用來(lái)結(jié)束創(chuàng)建的新進(jìn)程,在windows系統(tǒng)上相當(dāng)于調(diào)用TerminateProcess(),在posix系統(tǒng)上相當(dāng)于發(fā)送信號(hào)SIGTERM和SIGKILL。
def TestKillAndTerminate():
p=subprocess.Popen("notepad.exe")
t = 1
while(t <= 5):
time.sleep(1)
t +=1
p.kill()
#p.terminate()
print ("new process was killed")
5) p.communicate可以與新進(jìn)程交互,但是必須要在popen構(gòu)造時(shí)候?qū)⒐艿乐囟ㄏ颉?/strong>
def TestCommunicate():
import subprocess
cmd = "dir"
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdoutdata, stderrdata) = p.communicate()
if p.returncode != 0:
print (cmd + "error !")
#defaultly the return stdoutdata is bytes, need convert to str and utf8
for r in str(stdoutdata,encoding='utf8' ).split("\n"):
print (r)
print (p.returncode)
def TestCommunicate2():
import subprocess
cmd = "dir"
#universal_newlines=True, it means by text way to open stdout and stderr
p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
curline = p.stdout.readline()
while(curline != ""):
print (curline)
curline = p.stdout.readline()
p.wait()
print (p.returncode)
6) call函數(shù)可以認(rèn)為是對(duì)popen和wait的分裝,直接對(duì)call函數(shù)傳入要執(zhí)行的命令行,將命令行的退出code返回。
def TestCall():
retcode = subprocess.call("c:\\test.bat")
print (retcode)
7)subprocess.getoutput 和 subprocess.getstatusoutput ,基本上等價(jià)于subprocess.call函數(shù),但是可以返回output,或者同時(shí)返回退出code和output。
但是可惜的是好像不能在windows平臺(tái)使用,在windows上有如下錯(cuò)誤:'{' is not recognized as an internal or external command, operable program or batch file.
def TestGetOutput():
outp = subprocess.getoutput("ls -la")
print (outp)
def TestGetStatusOutput():
(status, outp) = subprocess.getstatusoutput('ls -la')
print (status)
print (outp)
8)總結(jié)
popen的參數(shù),第一個(gè)為字符串(或者也可以為多個(gè)非命名的參數(shù)),表示你要執(zhí)行的命令和命令的參數(shù);后面的均為命名參數(shù);shell=True,表示你前面的傳入的命令將在shell下執(zhí)行,如果你的命令是個(gè)可執(zhí)行文件或bat,不需要指定此參數(shù);stdout=subprocess.PIPE用來(lái)將新進(jìn)程的輸出重定向,stderr=subprocess.STDOUT將新進(jìn)程的錯(cuò)誤輸出重定向到stdout,stdin=subprocess.PIPE用來(lái)將新進(jìn)程的輸入重定向;universal_newlines=True表示以text的方式打開(kāi)stdout和stderr。
其他的不推薦使用的模塊:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
相關(guān)文章
解決Python selenium get頁(yè)面很慢時(shí)的問(wèn)題
今天小編就為大家分享一篇解決Python selenium get頁(yè)面很慢時(shí)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01使用python進(jìn)行文本預(yù)處理和提取特征的實(shí)例
今天小編就為大家分享一篇使用python進(jìn)行文本預(yù)處理和提取特征的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Tensorflow實(shí)現(xiàn)在訓(xùn)練好的模型上進(jìn)行測(cè)試
今天小編就為大家分享一篇Tensorflow實(shí)現(xiàn)在訓(xùn)練好的模型上進(jìn)行測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Pytho常見(jiàn)的數(shù)據(jù)可視化庫(kù),小白必備
Python作為數(shù)據(jù)分析的重要語(yǔ)言為數(shù)據(jù)分析的每個(gè)環(huán)節(jié)都提供了很多庫(kù).常見(jiàn)的數(shù)據(jù)可視化庫(kù)包括matplotib,seaborm,ggplot,bokeh,pygal,pyecharts等,下面小編一一介紹下,需要的朋友可以參考下2021-05-05Python通過(guò)調(diào)用有道翻譯api實(shí)現(xiàn)翻譯功能示例
這篇文章主要介紹了Python通過(guò)調(diào)用有道翻譯api實(shí)現(xiàn)翻譯功能,結(jié)合實(shí)例形式分析了基于Python實(shí)現(xiàn)的有道翻譯api調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-07-07動(dòng)態(tài)規(guī)劃之矩陣連乘問(wèn)題Python實(shí)現(xiàn)方法
這篇文章主要介紹了動(dòng)態(tài)規(guī)劃之矩陣連乘問(wèn)題Python實(shí)現(xiàn)方法,較為詳細(xì)的分析了矩陣連乘問(wèn)題的概念、原理并結(jié)合實(shí)例形式分析了Python相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11Python第三方庫(kù)OS庫(kù)方法實(shí)操
這篇文章主要給大家介紹了關(guān)于Python第三方庫(kù)OS庫(kù)的相關(guān)資料,os庫(kù)主要是對(duì)文件和文件夾進(jìn)行操作,在Python中對(duì)?件和?件夾的操作要借助os模塊??的相關(guān)功能,需要的朋友可以參考下2024-06-06解決PDF 轉(zhuǎn)圖片時(shí)丟文字的一種可能方式
這篇文章主要介紹了解決PDF 轉(zhuǎn)圖片時(shí)丟字的一種可能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03一小時(shí)學(xué)會(huì)TensorFlow2之全連接層
這篇文章主要介紹了TensorFlow2之全連接層,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09