Python subprocess question

Discussion in 'Python' started by Alexi Zuo, Jan 6, 2009.

  1. Alexi Zuo

    Alexi Zuo Guest

    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"
     
    Alexi Zuo, Jan 6, 2009
    #1
    1. Advertising

  2. Alexi Zuo

    Roger Guest

    On Jan 6, 7:46 am, Duncan Booth <> wrote:
    > "Alexi Zuo" <> 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?
     
    Roger, Jan 6, 2009
    #2
    1. Advertising

  3. Alexi Zuo

    Guest

    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://smontanaro.dyndns.org/
     
    , Jan 6, 2009
    #3
  4. Alexi Zuo

    Roger Guest

    On Jan 6, 1:18 pm, 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 - -http://smontanaro.dyndns.org/


    Gotcha. Thank you, that makes it clearer. =)
     
    Roger, Jan 6, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pekka Niiranen

    Python 2.4: Subprocess and Cygwin question

    Pekka Niiranen, Nov 21, 2004, in forum: Python
    Replies:
    0
    Views:
    301
    Pekka Niiranen
    Nov 21, 2004
  2. Replies:
    2
    Views:
    1,904
    Nick Craig-Wood
    Aug 24, 2006
  3. Calvin Spealman

    Subprocess with a Python Session?

    Calvin Spealman, Dec 5, 2006, in forum: Python
    Replies:
    1
    Views:
    379
    Nick Craig-Wood
    Dec 6, 2006
  4. Fredrik Lundh

    Re: Subprocess with a Python Session?

    Fredrik Lundh, Dec 5, 2006, in forum: Python
    Replies:
    7
    Views:
    596
    Hendrik van Rooyen
    Dec 9, 2006
  5. hiral
    Replies:
    2
    Views:
    631
    Jean-Michel Pichavant
    May 5, 2010
Loading...

Share This Page