7

メインループで他のことを処理しながら、スレッドを使用してストリーム (/dev/tty1) から文字列を読み取ります。CTRL-C を押したときにスレッドがメイン プログラムと一緒に終了するようにします。

   from threading import Thread

   class myReader(Thread):
      def run(self):
         with open('/dev/tty1', encoding='ascii') as myStream:
            for myString in myStream:
               print(myString)
      def quit(self):
         pass # stop reading, close stream, terminate the thread

   myReader = Reader()
   myReader.start()
   while(True):
      try:
         pass # do lots of stuff
      KeyboardInterrupt:
         myReader.quit()
         raise

通常の解決策 (run() ループ内のブール変数) は、ここでは機能しません。これに対処するための推奨される方法は何ですか?

Daemon フラグを設定することはできますが、後で (何らかのクリーンアップを行うために) 価値があると判明する可能性がある quit() メソッドを使用することはできません。何か案は?

4

2 に答える 2

6

私の知る限り、Python 3 にはそのための組み込みメカニズムはありません (Python 2 と同様)。実証済みの Python 2 アプローチを試したことがありますか?ここここPyThreadState_SetAsyncExcに文書化されていますか、または別のトレース アプローチをここで試しましたか?

PyThreadState_SetAsyncExc上記のアプローチを少し変更したバージョンを次に示します。

import threading
import inspect
import ctypes 
 
def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble, 
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")
 
def stop_thread(thread):
    _async_raise(thread.ident, SystemExit)
于 2011-06-15T12:41:06.863 に答える
4

スレッドをデーモンスレッドにします。デーモン以外のすべてのスレッドが終了すると、プログラムは終了します。したがって、Ctrl-Cがプログラムに渡され、メインスレッドが終了する場合、リーダーを明示的に強制終了する必要はありません。

    myReader = Reader()
    myReader.daemon = True
    myReader.start()
于 2011-09-12T03:12:39.667 に答える