Python Threading and sys.exit() codes

David Fisher python at rose164.wuh.wustl.edu
Fri Mar 31 23:52:07 EST 2000


----- Original Message -----
From: <dickenson at serviceware.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
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):
        thread.start_new_thread(worker,(jq,))
    for job in joblist:
        jq.put(job)

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
information.

Good luck,
David





More information about the Python-list mailing list