Python subprocess question

Alexi Zuo alexi.zuo at gmail.com
Tue Jan 6 04:12:21 EST 2009


Hi everyone,

Here I have a simple program which starts a thread and the thread use
Popen to execute a shell cmd which needs a long time.  I want to stop
the thread once I type "ctrl+C" (KeyboardInterrupt). But in fact this
program enters a dead loop. Can anyone tell me what is wrong?

Alex

from subprocess import *
import threading
import time

class TestThread(threading.Thread):
    def run(self):
        try:
            while True:
                p=Popen("for (( i = 0 ; i <= 100000; i++ )); do echo
hello; done", shell=True,stdin=PIPE,stdout=PIPE,stderr=PIPE,close_fds=True)
                print p.stdout.readlines()
                print p.stderr.readlines()
        except KeyboardInterrupt:
            print "got ex"
        print "leave..."

a=TestThread()
a.start()
try:
    a.join()
except:
    print "gone"



More information about the Python-list mailing list