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"

  

时间: 2024-08-04 11:25:17

python subprocess阻塞的相关文章

Python subprocess模块学习总结

从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.commands.*不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息 一.subprocess以及常用的封装函数 运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec

python subprocess

def getResult(cmd, timeout=2): #命令超时时间 deadline = time.time() + timeout r = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) while time.time() < deadline and r.poll() is None: time.sleep(0.1) if r.poll() is None: #检查子进程 r.kill() r.wait() ret

python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时

下面的资料是关于python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时时间的代码. import subprocess import os import time tt = '555' cmd = "python /home/100003/python/mypython/sub2.py "+" 333"+" 444 "+tt print time.time() sub2 = subprocess.Pope

通过阅读python subprocess源码尝试实现非阻塞读取stdout以及非阻塞wait

http://blog.chinaunix.net/uid-23504396-id-4661783.html 执行subprocess的时候,执行不是问题最麻烦的是获取进程执行后的回显来确认是否正确执行,还不能阻塞还要获取进程执行后的返回状态确认进程是否正确结束,也不能阻塞 分开解决这个问题我们先解决第一个问题,获取回显 一般获取回显,代码都是如下写法 点击(此处)折叠或打开 sub_process = subprocess.Popen(command, stdin = subprocess.P

python subprocess模块

转 http://blog.csdn.net/imzoer/article/details/8678029 subprocess的目的就是启动一个新的进程并且与之通信. subprocess模块中只定义了一个类: Popen.可以使用Popen来创建进程,并与进程进行复杂的交互.它的构造函数如下: subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=

Python subprocess shell 编程规范

使用subprocess通过shell调用另一个模块组件时,需要对返回的code进行判断.判断结果为执行失败时需要raise Exception,不然调用树过于复杂时,我们很难跟踪到异常发生的位置.sys.exit(1)虽然也可以达到对执行结果进行判断的目的,但是它难于追踪异常发生的位置. 1 a.py 2 `-- b.py 3 `-- ls a.py 1 import sys, subprocess 2 3 def exec_cmd(cmd): 4 """Run shell

python subprocess 杀掉全部派生的子进程

下面就是今天下午的研究成果. 发布系统需要响应用户的中断请求,需要在GET方法中杀掉由subprocess派生的子进程,刚开始直接用os.kill 发现子进程的子进程无法kill,谷歌了一些,发现kill可以干掉进程组,于是测试,但是默认情况下,subprocess派生的进程组和主程序,也就是我的web.py进程是在一个进程组里的,这要是kill了,那就调的了. 继续翻google,看subprocess的document时发现这个变量: subprocess.CREATE_NEW_PROCES

python subprocess select 读取

#!/usr/bin/env python # coding:utf-8 from __future__ import absolute_import, print_function import os import fcntl import select import subprocess from threading import Timer def make_nonblock(fd): flags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(f

python非阻塞

settimeout setblocking+select(了解select) 继续昨天的干活,这次我使用select+setblocking和settimeout来做个对比,以次来证明. 首先我设置socket为非阻塞的.然后使用select来监控套接字. #!/usr/bin/env python# encoding: utf-8import socketimport threadingimport Queueimport timeimport selectclass worker(thre