开发者

Python exec command with pipe control

I have a python script which is called by network. This script gets some data as input stream and sends some data as output one - stdin and stdout.

Now I want this script to call other one. And redirects his stdin to child process and child's stdout back. Basically this will be proxy like script.

The most important moment is data size to be transferred. It is huge - so I can not use buffers. For example: let's say child process will send 1GB of data. Parent script should be able to ready child's stdout and forward this data to his own stdout.

The same problem is with stdin - parent receives lots of data and can not buffer it before sendi开发者_StackOverflow中文版ng to child.

The question is how to implement this in python?

thank you


If data needs to be fully read before sending it back then you could implement a proxy using :

import sys
from subprocess import *
if __name__ == "__main__":

    network_content = sys.stdin.read()
    p = Popen("python other_script.py",stdin=PIPE, stdout=PIPE, stderr=PIPE, 
          close_fds=True)
    child_stdin,child_stdout = p.communicate()
    child_stdin.write(network_content)    
    child_content = child_stdout.read()
    sys.stdout.write(child_content)    

If keeping all the data in memory is an issue then read it in blocks and dump them in a TMP file and write to the child_stdout from that file - also in blocks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜