FW: [Tutor] Multi-Threading and KeyboardInterrupt

  • Thread starter Strax-Haber, Matthew (LARC-D320)
  • Start date
S

Strax-Haber, Matthew (LARC-D320)

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 <[email protected]>
Reply-To: Matthew Strax-Haber <[email protected]>
Date: Tue, 9 Jun 2009 22:01:33 -0500
To: Python Tutor <[email protected]>
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 = )
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 - (e-mail address removed)
http://mail.python.org/mailman/listinfo/tutor

------ End of Forwarded Message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top