FW: [Tutor] Multi-Threading and KeyboardInterrupt

Discussion in 'Python' started by Strax-Haber, Matthew (LARC-D320), Jun 11, 2009.

  1. 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 <>
    > Reply-To: Matthew Strax-Haber <>
    > Date: Tue, 9 Jun 2009 22:01:33 -0500
    > To: Python Tutor <>
    > 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 -
    > http://mail.python.org/mailman/listinfo/tutor
    >


    ------ End of Forwarded Message
    Strax-Haber, Matthew (LARC-D320), Jun 11, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Nestlerode

    KeyboardInterrupt and threading

    Ivan Nestlerode, Jan 2, 2004, in forum: Python
    Replies:
    8
    Views:
    508
    Anand Pillai
    Jan 8, 2004
  2. Replies:
    38
    Views:
    1,251
    Dennis Lee Bieber
    Feb 15, 2005
  3. PantherSE
    Replies:
    0
    Views:
    329
    PantherSE
    May 16, 2005
  4. akineko
    Replies:
    3
    Views:
    2,589
    Jesse Noller
    Jan 29, 2009
  5. Dennis Lee Bieber
    Replies:
    13
    Views:
    531
    Mike Kazantsev
    Jun 16, 2009
Loading...

Share This Page