欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python進(jìn)程類(lèi)subprocess的一些操作方法例子

 更新時(shí)間:2014年11月22日 16:24:59   投稿:junjie  
這篇文章主要介紹了python進(jìn)程類(lèi)subprocess的一些操作方法例子,本文講解了Popen、wait、poll、kill、communicate等方法的實(shí)際操作例子,需要的朋友可以參考下

subprocess.Popen用來(lái)創(chuàng)建子進(jìn)程。

1)Popen啟動(dòng)新的進(jìn)程與父進(jìn)程并行執(zhí)行,默認(rèn)父進(jìn)程不等待新進(jìn)程結(jié)束。

復(fù)制代碼 代碼如下:

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)程的返回值。

復(fù)制代碼 代碼如下:

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é)束。

復(fù)制代碼 代碼如下:

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。

復(fù)制代碼 代碼如下:

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>

復(fù)制代碼 代碼如下:

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返回。

復(fù)制代碼 代碼如下:

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.

復(fù)制代碼 代碼如下:

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)文章

最新評(píng)論