0

共通の例外ハンドラーを使用して、Python アプリケーションのすべてのスレッドの例外をキャッチすることは可能ですか?

次の例を考えてみましょう。メインスレッドからキャッチしたいのですCTRL+Cが、他のスレッドを終了しないワーカースレッドの1つにキャッチされることがあります。このような場合の出力は次のとおりです。

^CTraceback (most recent call last):
  File "thread-example.py", line 50, in <module>
    main()
  File "thread-example.py", line 30, in main
    t.join(1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 675, in join
    self.__block.wait(delay)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 263, in wait
    _sleep(delay)
KeyboardInterrupt

これはテストアプリケーションです。

#!/usr/bin/python

import os
import sys
import threading
import time
import subprocess
import signal


class Worker(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.kill_received = False
        self.daemon = True
        self.name = name

    def run(self):
        while not self.kill_received:
            time.sleep(1)
            print self.name

def main():
    threads = []

    for i in xrange(10):
        t = Worker('worker-%d' % i)
        threads.append(t)
        t.start()
        t.join(1)

    while len(threads) > 0:
        try:
            threads = [t for t in threads if t is not None and t.isAlive()]
            time.sleep(1)
        except KeyboardInterrupt:
            print "Ctrl-c received! Sending kill to threads..."
            for t in threads:
                t.kill_received = True
            # wait for threads to finish gracefully, then kill them anyway.
            # since all threads are daemons, they should finish once the main 
            # loop terminates
            for i in xrange(5):
                print '%i ...' % (5-i)
                time.sleep(1)
            print 'Exit!'
            os._exit(1)

if __name__ == '__main__':
    main()
4

1 に答える 1