Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6170779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:05:55+00:00 2026-05-23T23:05:55+00:00

I am trying to run a shell from my python program. I have used

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:05:57+00:00Added an answer on May 23, 2026 at 11:05 pm

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run a batch file, as another user, from my web app.
I run a python shell from crontab every minute: * * * * *
I'm trying to run pdflatex on a .tex file from a Python 2.4.4. subprocess
I'm trying to run a shell command using the backtick operators, but the fact
I'm trying to write a shell script that, when run, will set some environment
I'm trying to run a process and do stuff with its input, output and
i just installed python i am trying to run this script: import csv reader
I am trying to run a Django management command from cron. I am using
I have the following python code: from django.db import models from datetime import datetime
I'm trying to invoke a native Windows command from Cygwin using run , but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.