开发者

asynchronous writing and reading of a file

I have two processes.

  1. One processes is redirecting output of some unix command to a file on server side.The data is always appended to the file. e.g.

    find / > tmp.txt
    
  2. Another process is opening and reading the same file and storing it in a string 开发者_如何学Pythonand sending the entire string to the client.

Now, this things take simultaneously. I am using python.

Any suggestion as in what can be possible ways to implement this scenario. Please explain with sample code.

Thanks in advance.

Tazim.


If what you want is having the Output of a Unix command in a file and displaying it at the same time, you can [tee][1] it to stdout and read it from there, like:

>>> command_line = '/bin/find / |tee tmp.txt'
>>> args = shlex.split(command_line)
>>> p = subprocess.Popen(args,stdout=subprocess.PIPE)

From there you can either use commuicate() or directly read the stdout from the POpen object. See how it can be used here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜