Python Threading and sys.exit() codes

David Fisher python at
Sat Apr 1 06:52:07 CEST 2000

----- Original Message -----
From: <dickenson at>
Newsgroups: comp.lang.python
To: <python-list at>
Sent: Friday, March 31, 2000 1:26 PM
Subject: Python Threading and sys.exit() codes

> My problem is this: I want to run this program as a scheduled task
> under a job scheduler (currently testing cronDsys from #ifdef). The
> single-threaded version of the program posts an appropriate code to the
> environment via sys.exit(). The job scheduler traps this and takes
> conditional action. So far, so good.
> In the multi-threaded version of the program, the exit codes are not
> getting posted and the main process is not terminating cleanly, at
> least from the perspective of the job scheduler -- it appears to run
> until manually terminated from the scheduler control console although
> the NT command window disappears from the target desktop as expected.

Well for one thing, sys.exit() doesn't do what you think it does.
Sys.exit() raises an exception SystemExit which if not caught will cause the
thread to exit silently.  Read the docs for thread which threading is built
on.  I don't know of a way to catch an exception that another thread raises.
Doesn't mean there isn't one, just that I don't know one.

Another thing you look at is the Queue module.  That's what got my attention
because you are setting a global lock and poping stuff off of a fifo stack,
and Queue does all that for you. Here's some code:

import Queue
import thread    #yes, I know I'm supposed to use threading

def worker(jobqueue):
    while 1:
        job = jobqueue.get()    # this blocks until a job is available
        print job    # or whatever

def main(joblist):
    jq = Queue.Queue(0)
    for i in range(10):
    for job in joblist:

There are lots of things that could be prettified in this code, but I hope
it gives you some ideas.  You might try a second Queue to gather return

Good luck,

More information about the Python-list mailing list