Python user input in child process

前端 未结 1 1265
孤独总比滥情好
孤独总比滥情好 2021-01-20 16:26

I\'m trying to create a child process that can take input through raw_input() or input(), but I\'m getting an end of liner error EOFError: EOF when asking for input

相关标签:
1条回答
  • 2021-01-20 17:05

    My answer is taken from here: Is there any way to pass 'stdin' as an argument to another process in python?

    I have modified your example and it seems to work:

    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()
    
    0 讨论(0)
提交回复
热议问题