您的位置:68399皇家赌场 > 集群主机 > Python subprocess.Popen communicate() 和wait()使用上的分别

Python subprocess.Popen communicate() 和wait()使用上的分别

发布时间:2019-08-03 11:34编辑:集群主机浏览(168)

    #!/usr/bin/python
    ##皇家赌场 , get subprocess module
    import subprocess
     
    ## call date command ##
    p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
     
    ## Talk with date command i.e. read data from stdout and stderr. Store this info in tuple
    ## Interact with process: Send data to stdin. Read data from stdout and stderr,
    ## until end-of-file is reached.Wait for process to terminate. The optional input
    ## argument should be a string to be sent to the child process, or None,
    ## if no data should be sent to the child. ##
    (output, err) = p.communicate()
     
    ## Wait for date to terminate. Get return returncode ##
    p_status = p.wait()
    print "Command output : ", output
    print "Command exit status/return code : ", p_status
     
    ## from:

    python 捕获 shell/bash 脚本的输出结果实例,pythonbash

    #!/usr/bin/python
    ## get subprocess module
    import subprocess
     
    ## call date command ##
    p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
     
    ## Talk with date command i.e. read data from stdout and stderr. Store this info in tuple
    ## Interact with process: Send data to stdin. Read data from stdout and stderr,
    ## until end-of-file is reached.Wait for process to terminate. The optional input
    ## argument should be a string to be sent to the child process, or None,
    ## if no data should be sent to the child. ##
    (output, err) = p.communicate()
     
    ## Wait for date to terminate. Get return returncode ##
    p_status = p.wait()
    print "Command output : ", output
    print "Command exit status/return code : ", p_status
     
    ## from:

    以上正是小编为大家带来的python 捕获 shell/bash 脚本的输出结果实例全体内容了,希望大家多多扶助帮客之家~

    捕获 shell/bash 脚本的输出结果实例,pythonbash #!/usr/bin/python ## get subprocess module import subprocess ## call date command ## p = subprocess.Popen("date",...

    Python subprocess.Popen communicate() 和wait()使用上的界别,pythonpopenwait

    所以会纠结到那几个标题上是因为发掘在调用Popen的wait方法之后先后一贯未曾回来。google开采wait是有异常的大恐怕发生死锁的。为了把这些标题到底弄精晓,搜索一些材质过来看看:

    原稿链接:

    看样子外人的事例:

    后天遇上的一个主题素材。轻巧说正是,使用 subprocess 模块的 Popen 调用外表程序,假诺 stdoutstderr 参数是 pipe,况且程序输出超越操作系统的 pipe size时,要是选择 Popen.wait() 格局等待程序截至获取再次回到值,会导致死锁,程序卡在 wait() 调用上。

    ulimit -a 看到的 pipe size 是 4KB,那只是每页的分寸,查询获悉 linux 私下认可的 pipe size 是 64KB。

    看例子:

    #!/usr/bin/env python
    # coding: utf-8
    # [email protected]/04/28
    
    import subprocess
    
    def test(size):
        print 'start'
    
        cmd = 'dd if=/dev/urandom bs=1 count=%d 2>/dev/null' % size
        p = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
        #p.communicate()
        p.wait()
    
        print 'end'
    
    # 64KB
    test(64 * 1024)
    
    # 64KB   1B
    test(64 * 1024   1)
    

    先是测验输出为 64KB 大小的情状。使用 dd 发生了正要 64KB 的正儿八经输出,由 subprocess.Popen 调用,然后使用 wait() 等待 dd 调用甘休。可以见见科学的 startend 输出;然后测验比 64KB 多的动静,这种气象下只输出了 start,也正是说程序试行卡在了 p.wait() 上,程序死锁。具体输出如下:

    start
    end
    start
    

    那死锁难点怎么样制止吗?官方文书档案里引入使用 Popen.communicate()。那些办法会把出口放在内存,实际不是管道里,所以那时候上限就和内部存款和储蓄器大小有关了,一般不会有标题。何况只要要拿走程序再次来到值,能够在调用 Popen.communicate() 之后取 Popen.returncode 的值。

    敲定:若是使用 subprocess.Popen,就不利用 Popen.wait(),而使用 Popen.communicate() 来等待外界程序推行甘休。

    Popen.wait()¶

    Wait for child process to terminate.  Set and returnreturncode attribute.

    Warning

    This will deadlock when using stdout=PIPE and/orstderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data.  Use communicate() to avoid that.

    Popen.``communicate(input=None
    Interact with process: Send data to stdin.  Read data from stdout and stderr, until end-of-file is reached.  Wait for process to terminate. The optionalinput argument should be a string to be sent to the child process, orNone, if no data should be sent to the child.

    communicate() returns a tuple (stdoutdata, stderrdata).

    Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE.  Similarly, to get anything other thanNone in the result tuple, you need to give stdout=PIPE and/orstderr=PIPE too.

    Note

    The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

    subprocess 的二种办法:

    1)假如想调用之后直接阻塞到子程序调用停止:

    Depending on how you want to work your script you have two options. If you want the commands to block and not do anything while it is executing, you can just use subprocess.call.

    #start and block until done
    subprocess.call([data["om_points"], ">", diz['d'] "/points.xml"])
    
    
    2)非阻塞的时候方式:
    

    If you want to do things while it is executing or feed things into stdin, you can use communicate after the popen call.

    #start and process things, then wait
    p = subprocess.Popen(([data["om_points"], ">", diz['d'] "/points.xml"])
    print "Happens while running"
    p.communicate() #now wait
    

    As stated in the documentation, wait can deadlock, so communicate is advisable.

    subprocess.Popen communicate() 和wait()使用上的区分,pythonpopenwait 之所以会纠结到这么些标题上是因为开掘在调用Popen的wait方法之后先后一贯没...

    为此会纠结到这些标题上是因为发掘在调用Popen的wait方法之后先后一贯未有回来。google开掘wait是有非常大或许发生死锁的。为了把那些标题到底弄精晓,搜索一些素材过来看看:

    原来的小说链接:

    看到外人的例子:

    前些天遇上的二个主题素材。简单说便是,使用 subprocess 模块的 Popen 调用外表程序,假诺 stdoutstderr 参数是 pipe,并且程序输出超越操作系统的 pipe size时,假若应用 Popen.wait() 格局等待程序停止获取再次来到值,会以致死锁,程序卡在 wait() 调用上。

    ulimit -a 看到的 pipe size 是 4KB,那只是每页的大小,查询获悉 linux 暗中同意的 pipe size 是 64KB。

    看例子:

    #!/usr/bin/env python
    # coding: utf-8
    # yc@2013/04/28
    
    import subprocess
    
    def test(size):
        print 'start'
    
        cmd = 'dd if=/dev/urandom bs=1 count=%d 2>/dev/null' % size
        p = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
        #p.communicate()
        p.wait()
    
        print 'end'
    
    # 64KB
    test(64 * 1024)
    
    # 64KB   1B
    test(64 * 1024   1)
    

    本文由68399皇家赌场发布于集群主机,转载请注明出处:Python subprocess.Popen communicate() 和wait()使用上的分别

    关键词: 68399皇家赌场 Python