zoukankan      html  css  js  c++  java
  • sub

    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 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.

    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 than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

    Note The data read is buffered in memory, so do not use this method if the data size is large or unlimited.
    我们可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe):

    import subprocess
    child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
    child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE)
    out = child2.communicate()
    print(out)
    subprocess.PIPE实际上为文本流提供一个缓存区。child1的stdout将文本输出到缓存区,随后child2的stdin从该PIPE中将文本读取走。child2的输出文本也被存放在PIPE中,直到communicate()方法从PIPE中读取出PIPE中的文本。

    要注意的是,communicate()是Popen对象的一个方法,该方法会阻塞父进程,直到子进程完成。

    我们还可以利用communicate()方法来使用PIPE给子进程输入:

    import subprocess
    child = subprocess.Popen(["cat"], stdin=subprocess.PIPE)
    child.communicate("vamei")
    我们启动子进程之后,cat会等待输入,直到我们用communicate()输入"vamei"。

     http://www.aikaiyuan.com/4705.html

    https://buluo.qq.com/p/detail.html?bid=234299&pid=3596725-1483410241&from=share_copylink

  • 相关阅读:
    不删除数据库,只删除GridView的某一行!
    纯CSS无图打造圆角Table 无图制作圆角
    2009年总结与2010总体计划
    工作中的碰到的问题,以及处理过程:
    SQL Server 2005 不允许远程连接解决方法
    Visual Studio 2008项目模板丢失的解决办法
    C#格式化数值结果表
    准备把csdn的博客搬到这里
    生产系统中 RAC 数据库服务器 不要批量 gzip压缩
    不再更新的业务统计表
  • 原文地址:https://www.cnblogs.com/newpython/p/6246524.html
Copyright © 2011-2022 走看看