Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Shutting worker threads down gracefully after signal, portably

Reply
Thread Tools

Shutting worker threads down gracefully after signal, portably

 
 
Duncan Findlay
Guest
Posts: n/a
 
      01-10-2012
Suppose I've got a Python daemon that spawns a bunch of worker threads, waits for a singal (e.g. SIGTERM) and then shuts down the worker threads gracefully. What's the simplest way to do the signal handling portably across as many operating systems as possible (at least Linux and FreeBSD). Specifically, I'm interested in solutions where the main thread consumes no CPU, so no time.sleep(n) loops.

The most obvious solution (below) does not work with on FreeBSD, because the signal gets delivered to a different thread and signal.pause() doesn't return.

_shutdown = False

def sig_handler(signum, frame):
print 'handled'
global _shutdown
_shutdown = True

if __name__ == '__main__':

# Set up signal handling.
signal.signal(signal.SIGTERM, sig_handler)

# Start worker threads.
workers = [Worker() for i in xrange(NUM_THREADS)]
for worker in workers:
worker.start()

# Sleep until woken by a signal.
while not _shutdown:
signal.pause()

# Shutdown work threads gracefully.
for worker in workers:
worker.shutdown()

Any ideas? I've attached a more complete code sample.

Thanks
Duncan Findlay



 
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
Computer Freezes At "Shutting Down" After Having Done System Restore Martin Computer Support 12 07-11-2009 01:41 PM
Event message: Application is shutting down. Reason: Hosting environment is shutting down. Jack ASP .Net 2 07-18-2007 01:27 PM
Core 2 Duo PC keeps shutting down itself after 2 seconds and booting up nonstop ss6nn1@googlemail.com Computer Support 18 08-16-2006 09:59 PM
Worker threads seem to affect gui processing anthony.jayasekera@gmail.com Java 4 09-03-2005 03:21 PM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57