python subprocess阻塞
生活随笔
收集整理的這篇文章主要介紹了
python subprocess阻塞
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import select
import os
import subprocess
import time
import fcntlargs = ['python','./fetch_file2.py',ip,path]
proc = subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)def non_block_read(output): # 避免阻塞fd = output.fileno()fl = fcntl.fcntl(fd, fcntl.F_GETFL)fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)try:return output.read()except:return ""while proc.poll() is None: #fetch中rsync結(jié)束。但是fetch沒有結(jié)束(懷疑輸出過大) 導(dǎo)致這里一直是Nonepassprint proc.poll() # 殺死fetch進(jìn)程 返回-9
print proc.stderr.read() #阻塞<br>#方法1:
#non_block_read(proc.stderr) #防止阻塞<br>#方法2:
select_rfds = [ proc.stdout, proc.stderr]
(rfds, wfds, efds) = select.select(select_rfds, [],[])
if proc.stderr in rfds: #不存在。若select_rfds=[stderr],則阻塞在select上len = proc.stderr.read(10)if len == 0:print "empty"
else:print "proc.stderr"if proc.stdout in rfds:print "proc.stdout"
?
總結(jié)
以上是生活随笔為你收集整理的python subprocess阻塞的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决TeXstudio外部查看器失败方法
- 下一篇: (十一)模仿学习