3

我正在尝试创建一个可以通过 raw_input() 或 input() 获取输入的子进程,但是在要求输入时出现了线性错误EOFError: EOF的结尾。

我这样做是为了在 python 中试验多处理,我记得这很容易在 C 中工作。是否有一种解决方法,而不使用从主进程到子进程的管道或队列?我真的很想让孩子处理用户输入。

def child():
    print 'test' 
    message = raw_input() #this is where this process fails
    print message

def main():
    p =  Process(target = child)
    p.start()
    p.join()

if __name__ == '__main__':
    main()

我写了一些测试代码,希望能显示我想要实现的目标。

4

1 回答 1

4

我的答案取自这里:有没有办法将“stdin”作为参数传递给 python 中的另一个进程?

我修改了你的例子,它似乎工作:

from multiprocessing.process import Process
import sys
import os

def child(newstdin):
    sys.stdin = newstdin
    print 'test' 
    message = raw_input() #this is where this process doesn't fail anymore
    print message

def main():
    newstdin = os.fdopen(os.dup(sys.stdin.fileno()))
    p =  Process(target = child, args=(newstdin,))
    p.start()
    p.join()

if __name__ == '__main__':
    main()
于 2012-12-12T09:11:25.757 回答