AppKit の PyObjC バインディングを使用する Python スクリプトを作成しています。スクリプトはオブザーバーを shared に登録し、通知を処理するために AppKit.CFRunLoopRun() を呼び出しますNSWorkspace
。notificationCenter
from __future__ import print_function
from AppKit import *
import signal
shared_workspace = NSWorkspace.sharedWorkspace()
def on_did_activate_application(notification):
print('on_did_activate_application(notification = %s)' % notification)
notification_center = shared_workspace.notificationCenter()
did_activate_application_observer = notification_center.addObserverForName_object_queue_usingBlock_(
NSWorkspaceDidActivateApplicationNotification,
None,
None,
on_did_activate_application)
def handle_interrupt(signum, frame):
notification_center.removeObserver_(did_activate_application_observer)
CFRunLoopStop(CFRunLoopGetCurrent())
signal.signal(signal.SIGINT, handle_interrupt)
CFRunLoopRun()
私が経験している問題 (上記の MCVE で再現可能) は、スクリプトを実行しているターミナル ウィンドウで Ctrl+C を押すと、handle_interrupt() がすぐに実行されず、次回NSWorkspaceDidActivateApplicationNotification
通知が処理されるときに実行されることです。
Ctrl+C / SIGINT が発生したらすぐに応答するにはどうすればよいですか?