我正在尝试创建一个使用python的多处理模块的脚本.脚本(让我们称之为myscript.py)将从另一个带有管道的脚本获取输入.
假设我像这样调用脚本;
$ python writer.py | python myscript.py
Run Code Online (Sandbox Code Playgroud)
这是代码;
// 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()
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为sys.stdin对象对于主进程和p1是不同的.所以我试过这个解决它,
//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()
Run Code Online (Sandbox Code Playgroud)
但我遇到了这个错误;
Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", …Run Code Online (Sandbox Code Playgroud)