I'm trying to follow the advice in Apple's documentation for stopping an NSRunLoop:
"If you want the run loop to terminate, you shouldn't use 'run'. Instead, use one of the other run methods and also check other arbitrary conditions of your own, in a loop. A simple example would be:"
BOOL shouldKeepRunning = YES; /NSRunLoop *theRL = [NSRunLoop currentRunLoop];while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);I'm having trouble translating that to python. It still runs forever. Noticeably, the process maxes out a core after the time interval is up.
from Cocoa import NSObject, NSSpeechRecognizer, NSRunLoop, NSDefaultRunLoopMode, NSDate
shouldKeepRunning = True
theFuture = NSDate.alloc().initWithTimeIntervalSinceNow_(10)
class Controller (NSObject):
def init(self):
commands = [ "up", "down" ]
self.recognizer = NSSpeechRecognizer.alloc().init()
self.recognizer.setCommands_(commands)
self.recognizer.startListening()
self.recognizer.setDelegate_(self)
def speechRecognizer_didRecognizeCommand_(self, recognizer, command):
shouldKeepRunning = False
return command
controller = Controller.alloc().init()
loop = NSRunLoop.currentRunLoop()
while shouldKeepRunning and (loop.runMode_beforeDate_(NSDefaultRunLoopMode, theFuture)):
passAlso I'm not convinced I can retrieve the command from out of the class. Any thoughts? Thanks.