Python 2.7 子进程中的Python用户输入

Python 2.7 子进程中的Python用户输入,python-2.7,multiprocessing,user-input,Python 2.7,Multiprocessing,User Input,我试图创建一个子进程,该进程可以通过raw_input()或input()获取输入,但在请求输入时,我得到了一个结束行错误eoferor:EOF 我这样做是为了在python中试验多处理,我记得这在C中很容易实现。是否有一种不使用从主进程到其子进程的管道或队列的解决方法?我真的希望孩子处理用户输入 def child(): print 'test' message = raw_input() #this is where this process fails print

我试图创建一个子进程,该进程可以通过raw_input()或input()获取输入,但在请求输入时,我得到了一个结束行错误eoferor: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()

我编写了一些测试代码,希望能显示我正在努力实现的目标。

我的答案如下:

我修改了您的示例,它似乎有效:

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()

这对你有用吗?I get ValueError:对关闭的文件执行I/O操作是的。可能行为取决于操作系统。你在用什么操作系统?我使用的是Linux.Windows 8,我想这是一个问题。这可能不会有帮助,但值得一试:检查如果将os.dup()的返回值作为newstdin传递,并将os.fdopen()移动到child()函数,会发生什么。这在某种程度上是可行的,但孩子现在无法退出原始输入(),并且整个脚本在3~4秒后奇怪地存在,而且打印语句都不起作用。