【发布时间】:2018-05-01 18:40:36
【问题描述】:
我有一个小型 python 服务器来执行以下工作:
- 主线程正在使用 raw_input 获取用户输入;
- 另一个线程(即后台线程)由主线程启动,以执行与用户输入对应的作业。作业都是用 bash 脚本编写的,所以这个线程使用 subprocess.Popen 来运行脚本。
高级代码是这样的(只是抽象一下,因为详细的代码太长,这里就不复制了……):
# A global queue is initialized to cache user inputs
command_queue = Queue.Queue()
# This is the background-thread running bash script based on user's input
class command_runner(Thread):
def run(self):
user_command = command_queue.get()
# code to run bash script specified in user_command, using Popen
class main_program(object):
def listen_user_input(self):
command_runner.start()
while True:
user_input = raw_input("Please input command:")
command_queue.put(user_input)
我注意到当后台线程 (command_runner) 运行一些 bash 脚本时,主程序的 raw_input 有时不响应任何用户输入。 raw_input 好像卡住了。
其他一些可能有用的信息:
- command_running 运行的 bash 脚本是长时间运行但不是繁重的脚本(通常约 10 分钟)。所以当 raw_input 卡住时,机器并没有耗尽资源。
- 某些脚本可能会使用“ssh -q some_command 2>&1”通过 ssh 连接到其他机器(因此 ssh 命令会安静运行)。
- bash 脚本的所有 stdout 和 stderr 都通过“Popen(user_command, stderr=sys.stdout.fileno(), stdout=log_fp)”重定向到一个文件
【问题讨论】:
-
如果您将
stdin=subprocess.PIPE添加到Popen,此错误是否仍然存在?想知道子进程是否继承了 Python 的stdin并从raw_input窃取它 -
@scnerd 似乎您的解决方案有效!你能解释一下吗(也许只是回复,所以我会接受你的回答~)非常感谢!
标签: raw-input