在儿童过程中使用stdin
因此,我有一个程序,在“主要”过程中,我启动了一个新的进程对象,该对象(我想要的)是从stdin
中读取行,并将它们附加到queue
对象。
从本质上讲,基本系统设置是有一个“命令获取”进程,用户将输入命令/查询,我需要将这些查询获取到在单独的过程中运行的其他子系统。我的想法是通过多处理。Suqueue
共享其他系统可以从中读取的。
我拥有的(仅关注获取命令/查询)基本上是:
def sub_proc(q):
some_str = ""
while True:
some_str = raw_input("> ")
if some_str.lower() == "quit":
return
q.put_nowait(some_str)
if __name__ == "__main__":
q = Queue()
qproc = Process(target=sub_proc, args=(q,))
qproc.start()
qproc.join()
# now at this point q should contain all the strings entered by the user
问题是我得到的:
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 "/home/blah/blah/blah/blah.py", line 325, in sub_proc
some_str = raw_input("> ")
File "/randompathhere/eclipse/plugins/org.python.pydev_2.1.0.2011052613/PySrc/pydev_sitecustomize/sitecustomize.py", line 181, in raw_input
ret = original_raw_input(prompt)
EOFError: EOF when reading a line
如何?
So I have a program, in the "main" process I fire off a new Process object which (what I want) is to read lines from stdin
and append them to a Queue
object.
Essentially the basic system setup is that there is a "command getting" process which the user will enter commands/queries, and I need to get those queries to other subsystems running in separate processes. My thinking is to share these via a multiprocessing.Queue
which the other systems can read from.
What I have (focusing on just the getting the commands/queries) is basically:
def sub_proc(q):
some_str = ""
while True:
some_str = raw_input("> ")
if some_str.lower() == "quit":
return
q.put_nowait(some_str)
if __name__ == "__main__":
q = Queue()
qproc = Process(target=sub_proc, args=(q,))
qproc.start()
qproc.join()
# now at this point q should contain all the strings entered by the user
The problem is that I get:
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 "/home/blah/blah/blah/blah.py", line 325, in sub_proc
some_str = raw_input("> ")
File "/randompathhere/eclipse/plugins/org.python.pydev_2.1.0.2011052613/PySrc/pydev_sitecustomize/sitecustomize.py", line 181, in raw_input
ret = original_raw_input(prompt)
EOFError: EOF when reading a line
How do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通过将原始的stdin文件描述符传递给子过程并在此处重新打开的问题来解决了一个类似的问题。
这适用于我相对简单的情况。我什至能够在重新开放的流中使用Readline模块。我不知道它对于更复杂的系统有多稳健。
I solved a similar issue by passing the original stdin file descriptor to the child process and re-opening it there.
This worked for my relatively simple case. I was even able to use the readline module on the re-opened stream. I don't know how robust it is for more complex systems.
简而言之,主过程和您的第二个过程没有共享相同的stdin。
运行它,您应该为sys.stdin.fileno()(
不幸的是,这都无法解决问题。你想做什么?
In short, the main process and your second process don't share the same STDIN.
Run that and you should get two different results for sys.stdin.fileno()
Unfortunately, that doesn't solve your problem. What are you trying to do?
如果您不想将stdin传递到目标过程功能,例如在 @ashelly的答案中,或者只需要在许多不同的过程中进行操作,则可以通过initializer使用
Multiprocessing.pool
进行操作。参数:上面的示例将打印数字9,然后打印输入提示,然后打印输入号码的平方。
请注意不要同时从同一描述符中读取多个子过程,否则可能会变得困惑。
If you don't want to pass stdin to the target processes function, like in @Ashelly's answer, or just need to do it for many different processes, you can do it with
multiprocessing.Pool
via the initializer argument:the above example will print the number 9, followed by a prompt for input and then the square of the input number.
Just be careful not to have multiple child processes reading from the same descriptor at the same time or things may get... confusing.
您可以使用螺纹并将其全部保持在相同的过程中:
You could use threading and keep it all on the same process: