Popen.stdin 存在多线程问题,无法正常工作
我正在尝试从我的 python 程序运行 shell。 我使用了多线程方法,其中接受用户的输入并应通过 shell 执行。
一切似乎都正确,除了程序执行不会发生在标准输入之外。
我不确定我使用 Popen.stdin 的方式是否有问题。
所以请帮忙解决这里出了什么问题。
from subprocess import Popen,PIPE
import shlex
import threading
from Queue import Queue
class MyThread(threading.Thread):
def __init__(self,func,args):
threading.Thread.__init__(self)
self.func=func
self.args=args
def run(self):
apply(self.func,self.args)
def bash(command,output):
commandList=shlex.split('python test.py')
process=Popen(commandList,stdout=PIPE,stdin=PIPE,stderr=PIPE,shell=True)
print process.stdout.readlines()
while (process.pole()==None):
#commandList=shlex.split(command.get(1))
print 'bash'
process.stdin.write(command.get(1))
process.stdin.flush()
output.put(process.stdout.readlines(),1)
process.stdout.flush()
def communicate(command,output):
while True:
command.put(raw_input('enter command>'))
print 'communicate'
print output.get(1)
funcs=[bash,communicate]
nfuncs=len(funcs)
def main():
command=Queue(1)
output=Queue(1)
threads=[]
for i in range(nfuncs):
t=MyThread(funcs[i],(command,output))
threads.append(t)
for i in range(nfuncs):
threads[i].start()
for i in range(nfuncs):
threads[i].join()
print 'successful'
if __name__=='__main__':
main()
我给出了下面的输出。
karthik@ubuntu:~/TerminalChatBot/test$ python threadTerminal.py
enter command>ls
communicate
此后不再执行。我什至无法使用 ctrl+c 来停止 python 脚本。它只是挂着。
注意:线程通信需要存在,因为我们需要将此代码与更大的模块集成。
I am trying to run a shell from my python program.
I have used a mltithreaded approach where an input from user is accepted and should be executed via the shell.
Everything seems right, except that the program execution just doesn't take place beyond stdin.
I am not sure if there is something wrong with the way I have used Popen.stdin.
So please help on what is wrong here.
from subprocess import Popen,PIPE
import shlex
import threading
from Queue import Queue
class MyThread(threading.Thread):
def __init__(self,func,args):
threading.Thread.__init__(self)
self.func=func
self.args=args
def run(self):
apply(self.func,self.args)
def bash(command,output):
commandList=shlex.split('python test.py')
process=Popen(commandList,stdout=PIPE,stdin=PIPE,stderr=PIPE,shell=True)
print process.stdout.readlines()
while (process.pole()==None):
#commandList=shlex.split(command.get(1))
print 'bash'
process.stdin.write(command.get(1))
process.stdin.flush()
output.put(process.stdout.readlines(),1)
process.stdout.flush()
def communicate(command,output):
while True:
command.put(raw_input('enter command>'))
print 'communicate'
print output.get(1)
funcs=[bash,communicate]
nfuncs=len(funcs)
def main():
command=Queue(1)
output=Queue(1)
threads=[]
for i in range(nfuncs):
t=MyThread(funcs[i],(command,output))
threads.append(t)
for i in range(nfuncs):
threads[i].start()
for i in range(nfuncs):
threads[i].join()
print 'successful'
if __name__=='__main__':
main()
I have given the output below.
karthik@ubuntu:~/TerminalChatBot/test$ python threadTerminal.py
enter command>ls
communicate
After this there is no execution. I can't even use ctrl+c to stop the python script. It just hangs.
NOTE: the thread communicate needs to be there as we need to integrate this code with the bigger module.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
小事情:
它是
process.poll()
而不是process.pole()
。而不是
do
为什么要运行 python test.py Now?您不想在 shell 中运行 ls communications 吗?在这种情况下您应该执行以下操作:
Minor things:
It's
process.poll()
notprocess.pole()
.Instead of
do
Now, why are you running
python test.py
? Don't you want to runls communicate
in a shell, in which case you should be doing something like:难道它一直挂在
process.stdout.readlines()
上吗?也许找不到行结尾。尝试 stdout.read(1) 读取一个字符,看看会发生什么。could it be that it keeps hanging on
process.stdout.readlines()
? Perhaps no line ending can be found. Try stdout.read(1) to read one character and see what happens.