python read函数阻塞_python subprocess阻塞

import select

import os

import subprocess

import time

import fcntl

args = ['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结束。但是fetch没有结束(怀疑输出过大) 导致这里一直是None

pass

print proc.poll() # 杀死fetch进程 返回-9

print proc.stderr.read() #阻塞

#方法1:

#non_block_read(proc.stderr) #防止阻塞

#方法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"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值