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 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()
import sys, time
try:
while True: pass
except KeyboardInterrupt:
print 'key interrupt %s' % sys.argv[1]
raise
------ End of Forwarded Message
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
#!/usr/bin/env pythonHey 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):
##############################################################################
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()
#!/usr/bin/env python##############################################################################
Sub.py (called by DB.py):
##############################################################################
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