FW: [Tutor] Multi-Threading and KeyboardInterrupt

Strax-Haber, Matthew (LARC-D320) matthew.strax-haber at nasa.gov
Thu Jun 11 09:44:24 EDT 2009


I sent this to the Tutor mailing list and did not receive a response.
Perhaps one of you might be able to offer some sagely wisdom or pointed
remarks?

Please reply off-list and thanks in advance. Code examples are below in
plain text.

------ Forwarded Message
> From: Matthew Strax-Haber <strax-haber.m at neu.edu>
> Reply-To: Matthew Strax-Haber <Matthew.Strax-Haber at nasa.gov>
> Date: Tue, 9 Jun 2009 22:01:33 -0500
> To: Python Tutor <tutor at python.org>
> Subject: [Tutor] Multi-Threading and KeyboardInterrupt
> 

> Hey everyone,
> 
> I hope one of you can help me with this. This is my first foray into
> multi-threaded programming. I have tried to boil my code down to it's
> simplest demonstrative form.
> 
> My program runs interactively by allowing the user to directly
> interact with the python prompt. This program has a runAll() method
> that runs a series of subprocesses with a cap on how many instances
> are running at a time. My intent is to allow the user to use Ctrl-C to
> break these subprocesses. Note that while not reflected in the demo
> below, each subprocess needs to be able to run clean-up code before
> shutting down (in single-threaded mode I just wrap in try-finally).
> When I run DB.py, and interrupt it with Ctrl-C, things do not run so
> cleanly. Please let me know what I can change to make this work
> properly. My intent is to have the following output:
> 
> 'key interrupt 1'
> 'key interrupt 2'
> 'key interrupt 3'
> 'key interrupt 4'
> '********* stopped midway ********'
> 
> Here is the code for a demo:
> ##############################################################################
> DB.py (run this):
> ##############################################################################
#!/usr/bin/env python

from subprocess    import Popen
from threading    import Thread, Semaphore

MAX_SUBPROCS    = 3
RUN_PERMISSION    = Semaphore(MAX_SUBPROCS)

def runSingle(i):
    with RUN_PERMISSION:
        Popen(['./Sub.py', str(i)]).wait()
        
def runAll():
    workers = [ Thread(target = runSingle, args = [i])
            for i in xrange(MAX_SUBPROCS + 1) ]
    try:
        for w in workers:
            w.start()
    except KeyboardInterrupt:
        ## I want this to be shown on a KeyboardInterrupt
        print '********* stopped midway ********'
    for w in workers:
        w.join()
            
runAll()
> ##############################################################################
> Sub.py (called by DB.py):
> ##############################################################################
#!/usr/bin/env python

import sys, time

try:
        while True: pass
except KeyboardInterrupt:
        print 'key interrupt %s' % sys.argv[1]
        raise
> ##############################################################################
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

------ End of Forwarded Message




More information about the Python-list mailing list