Skip to content

Instantly share code, notes, and snippets.

@emilyst
Created March 19, 2019 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyst/19c5d2c2f8cdba4d775d6d00c6115e38 to your computer and use it in GitHub Desktop.
Save emilyst/19c5d2c2f8cdba4d775d6d00c6115e38 to your computer and use it in GitHub Desktop.
This doesn't work
#!/usr/bin/env python3
import threading
# from multiprocessing import Pool
# from multiprocessing import Process
class EventLoopThread(threading.Thread):
def __init__(self):
super(EventLoopThread, self).__init__()
self.setDaemon(True)
self.should_stop = False
def run(self):
from AppKit import (
# NSDate,
NSLog,
NSObject,
# NSRunLoop,
NSWorkspace,
NSWorkspaceDidWakeNotification,
NSWorkspaceWillSleepNotification,
)
# from PyObjCTools import AppHelper
from CoreFoundation import (
CFRunLoopGetCurrent,
CFRunLoopRun,
CFRunLoopStop,
)
class SleepWakeNotificationHandler(NSObject):
def receiveSleepNotification_(self, notification):
NSLog('receiveSleepNotification: %@', notification)
def receiveWakeNotification_(self, notification):
NSLog('receiveWakeNotification: %@', notification)
NSLog('Adding observers')
workspace = NSWorkspace.sharedWorkspace()
notificationCenter = workspace.notificationCenter()
notificationHandler = SleepWakeNotificationHandler.new()
notificationCenter.addObserver_selector_name_object_(
notificationHandler,
'receiveSleepNotification:',
NSWorkspaceWillSleepNotification,
None
)
notificationCenter.addObserver_selector_name_object_(
notificationHandler,
'receiveWakeNotification:',
NSWorkspaceDidWakeNotification,
None
)
NSLog('Starting event loop')
# NSRunLoop.currentRunLoop().runUntilDate_(NSDate.distantFuture())
CFRunLoopRun()
# AppHelper.runConsoleEventLoop()
def stop(self):
NSLog('Stopping the event loop')
CFRunLoopStop(CFRunLoopGetCurrent())
event_loop_thread = EventLoopThread()
event_loop_thread.start()
event_loop_thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment