有什么办法可以将"stdin"作为参数传递给python中的另一个进程? [英] Is there any way to pass 'stdin' as an argument to another process in python?

查看:143
本文介绍了有什么办法可以将"stdin"作为参数传递给python中的另一个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本在python中使用多处理模块.该脚本(我们称其为myscript.py)将从带有管道的另一个脚本获取输入.

I'm trying to create a script which is using multiprocessing module with python. The script (lets call it myscript.py) will get the input from another script with pipe.

假设我这样调用脚本;

Assume that I call the scripts like this;

$ python writer.py | python myscript.py 

这是代码;

// writer.py
import time, sys

def main():
    while True:
        print "test"
        sys.stdout.flush()
        time.sleep(1)

main()

//myscript.py
def get_input():
    while True:
        text = sys.stdin.readline()
        print "hello " + text
        time.sleep(3)

if __name__ == '__main__':        
    p1 = Process(target=get_input, args=())
    p1.start()

这显然不起作用,因为sys.stdin对象对于主进程和p1是不同的.因此,我已尝试解决此问题,

this is clearly not working, since the sys.stdin objects are different for main process and p1. So I have tried this to solve it,

//myscript.py
def get_input(temp):
    while True:
        text = temp.readline()
        print "hello " + text
        time.sleep(3)

if __name__ == '__main__':        
    p1 = Process(target=get_input, args=(sys.stdin,))
    p1.start()

但是我遇到了这个错误;

but I come across with this error;

Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "in.py", line 12, in get_input
    text = temp.readline()
ValueError: I/O operation on closed file

因此,我想main的stdin文件已关闭,我无法读取它.在这种结合下,如何将main的stdin文件传递给另一个进程?如果无法传递标准输入,如何在另一个进程中使用main的标准输入?

So, I guess that main's stdin file closed and I can't read from it. At this conjunction, how can I pass main's stdin file to another process? If passing stdin is not possible, how can I use main's stdin from another process?

更新: 好的,我需要澄清我的问题,因为人们认为使用多处理并不是真正必要的. 像这样考虑myscript.py;

update: Okay, I need to clarify my question since people think using multiprocessing is not really necessary. consider myscript.py like this;

//myscript.py
def get_input():
    while True:
        text = sys.stdin.readline()
        print "hello " + text
        time.sleep(3)

def do_more_things():
    while True:
        #// some code here
        time.sleep(60*5)

if __name__ == '__main__':        
    p1 = Process(target=get_input, args=())
    p1.start()

    do_more_things()

所以,我真的需要与主函数(或其他子进程)并行运行get_input()函数. 对不起,冲突,我的英语很体面,我想我对这个问题不清楚.如果您能告诉我是否可以在另一个进程中使用主进程STDIN对象,我将不胜感激.

so, I really need to run get_input() function parallelly with main function (or other sub processes). Sorry for the conflicts, I have a decent English, and I guess I couldn't be clear on this question. I would appreciate if you guys can tell me if i can use the main processes STDIN object in another process.

提前谢谢.

推荐答案

最简单的方法是交换get_input()do_more_things(),即在父进程中读取sys.stdin:

The simplest thing is to swap get_input() and do_more_things() i.e., read sys.stdin in the parent process:

def get_input(stdin):
    for line in iter(stdin.readline, ''):
        print("hello", line, end='')
    stdin.close()

if __name__ == '__main__':
    p1 = mp.Process(target=do_more_things)
    p1.start()
    get_input(sys.stdin)

下一个最好的方法是对get_input()使用Thread()而不是Process():

The next best thing is to use a Thread() instead of a Process() for get_input():

if __name__ == '__main__':
    t = Thread(target=get_input, args=(sys.stdin,))
    t.start()
    do_more_things()

如果上述方法不能解决问题,您可以尝试os.dup():

If the above doesn't help you could try os.dup():

newstdin = os.fdopen(os.dup(sys.stdin.fileno()))
try: 
   p = Process(target=get_input, args=(newstdin,))
   p.start()    
finally:
   newstdin.close() # close in the parent
do_more_things()

这篇关于有什么办法可以将"stdin"作为参数传递给python中的另一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆