def __process_queues(self, timer, context): # Die when asked to. self.lock.acquire() if self.die: CFRunLoopStop(CFRunLoopGetCurrent()) self.lock.release() # Process add queue. self.lock.acquire() if not self.add_queue.empty(): (path, event_mask) = self.add_queue.get() self.lock.release() self.__add_dir(path, event_mask) else: self.lock.release() # Process remove queue. self.lock.acquire() if not self.remove_queue.empty(): path = self.add_queue.get() self.lock.release() self.__remove_dir(path) else: self.lock.release() # Ensure all monitored paths are actually being monitored. If they're # not yet being monitored, start doing so. for path in self.monitored_paths.keys(): if self.monitored_paths[path].monitoring: continue streamRef = self.monitored_paths[path].fsmonitor_ref # Schedule stream on a loop. FSEventStreamScheduleWithRunLoop(streamRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode) # Register with the FS Events service to receive events. started = FSEventStreamStart(streamRef) if not started: raise CouldNotStartError else: self.monitored_paths[path].monitoring = True # Generate the missed events. # TODO: use FSEvents' sinceWhen parameter instead of the current # inefficient scanning method. if self.persistent: FSMonitor.generate_missed_events(self, path)
def stop(self): if self._run_loop is not None: CFRunLoopStop(self._run_loop)