Popen.stdin 存在多线程问题,无法正常工作

发布于 2024-11-25 10:24:19 字数 1733 浏览 0 评论 0原文

我正在尝试从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-12-02 10:24:19

小事情:

它是 process.poll() 而不是 process.pole()

而不是

 for i in range(nfuncs):
    t=MyThread(funcs[i],(command,output))
    threads.append(t)

do

 for func in nfuncs:
    t=MyThread(func,(command,output))
    threads.append(t)

为什么要运行 python test.py Now?您不想在 shell 中运行 ls communications 吗?在这种情况下您应该执行以下操作:

def bash(command,output):
    process=Popen('bash',stdout=PIPE,stdin=PIPE,stderr=PIPE,shell=True)
    print process.stdout.readlines()
    # I don't really understand what's going on here
    while (process.pole()==None):
        print 'bash'
        process.stdin.write(command.get(1))
        process.stdin.flush()
        output.put(process.stdout.readlines(),1)
        process.stdout.flush()

Minor things:

It's process.poll() not process.pole().

Instead of

 for i in range(nfuncs):
    t=MyThread(funcs[i],(command,output))
    threads.append(t)

do

 for func in nfuncs:
    t=MyThread(func,(command,output))
    threads.append(t)

Now, why are you running python test.py? Don't you want to run ls communicate in a shell, in which case you should be doing something like:

def bash(command,output):
    process=Popen('bash',stdout=PIPE,stdin=PIPE,stderr=PIPE,shell=True)
    print process.stdout.readlines()
    # I don't really understand what's going on here
    while (process.pole()==None):
        print 'bash'
        process.stdin.write(command.get(1))
        process.stdin.flush()
        output.put(process.stdout.readlines(),1)
        process.stdout.flush()
倾`听者〃 2024-12-02 10:24:19

难道它一直挂在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文