进程中的Python命令行输入 [英] Python command line input in a process

查看:84
本文介绍了进程中的Python命令行输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统需要从几个不同的进程接收输入.最简单的只是命令行,用户可以在其中手动输入数据.这些数据将被添加到multiprocessing.Queue中,并在以后由主进程处理,但我还没走那么远.在进程内调用raw_input似乎无效.我抽出了代码的精髓,下面是一个示例:

I've got a system that needs to receive input from a few different processes. The simplest is just a command line where the user enters data manually. This data will be added to a multiprocessing.Queue and handled later by the main process, but I'm not even getting that far; calling raw_input inside a process doesn't seem to work. I pulled out the meat of the code and here's an example:

import multiprocessing

def f():
    while True:
        raw_input('>>>')

p = multiprocessing.Process(target = f)
p.start()

这个简单的代码抛出了这个:

This simple code throws this:

~$ python test.py
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.6/multiprocessing/process.py", line 232, in _bootstrap
    self.run()
  File "/usr/lib/python2.6/multiprocessing/process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "test.py", line 5, in f
    raw_input('>>>')
EOFError: EOF when reading a line
>>>~$

如何在Python的进程中获取命令行输入?

How can I get command line input in a process in Python?

推荐答案

在Python中生成线程时,

When you spawn a thread in Python, it closes stdin. You can't use a subprocess to collect standard input. Use the main thread to collect input instead and post them to the Queue from the main thread. It may be possible to pass the stdin to another thread, but you likely need to close it in your main thread.

这篇关于进程中的Python命令行输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆