Python subprocess question

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?

Advertisements

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?

Advertisements

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.

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/

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!