Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python subprocess question

Reply
Thread Tools

Python subprocess question

 
 
Alexi Zuo
Guest
Posts: n/a
 
      01-06-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,clos e_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"
 
Reply With Quote
 
 
 
 
Roger
Guest
Posts: n/a
 
      01-06-2009
On Jan 6, 7:46*am, Duncan Booth <(E-Mail Removed)> wrote:
> "Alexi Zuo" <(E-Mail Removed)> wrote:
> > 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?

>
> The KeyboardInterrupt exception is being raised in your main thread which
> doesn't handle it so it just terminates. Your child thread isn't marked as
> a daemon thread so the program won't exit until the child thread has also
> terminated.


I've been recently working with threads for the first time and I've
been unclear on the setDaemon flag though I've sifted through the
documentation for a clear answer (most likely my reading comprehension
has failed me and not the documentation). I think I had the notion of
that flag backwards. .setDaemon(True) means the thread gets destroyed
when the program exits and default .setDaemon(False) means that the
thread continues to process even when the main program is gone?
 
Reply With Quote
 
 
 
 
skip@pobox.com
Guest
Posts: n/a
 
      01-06-2009

Roger> .setDaemon(True) means the thread gets destroyed when the program
Roger> exits and default .setDaemon(False) means that the thread
Roger> continues to process even when the main program is gone?

Approximately. The main thread (and thus the program) will exit only when
all non-daemon threads have finished. A thread can't continue to run if the
main program has exited.

--
Skip Montanaro - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://smontanaro.dyndns.org/
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      01-06-2009
On Jan 6, 1:18*pm, (E-Mail Removed) wrote:
> * * Roger> .setDaemon(True) means the thread gets destroyed when the program
> * * Roger> exits and default .setDaemon(False) means that the thread
> * * Roger> continues to process even when the main program is gone?
>
> Approximately. *The main thread (and thus the program) will exit only when
> all non-daemon threads have finished. *A thread can't continue to run if the
> main program has exited.
>
> --
> Skip Montanaro - (E-Mail Removed) -http://smontanaro.dyndns.org/


Gotcha. Thank you, that makes it clearer. =)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to import subprocess into my 'subprocess.py' file hiral Python 2 05-05-2010 12:56 PM
Subprocess with a Python Session? Calvin Spealman Python 1 12-06-2006 06:33 AM
Can Python do Perl's print <<EOF; notation? - popen, subprocess works? yichun.wei@gmail.com Python 2 08-24-2006 09:30 AM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
Python 2.4: Subprocess and Cygwin question Pekka Niiranen Python 0 11-21-2004 09:10 PM



Advertisments