def startCamera(folder, lapDuration): task = None camera = VideoStream(src=0).start() shown = False while True: # Init window if not shown: print("Initialize camera and snap task") cv2.namedWindow("Camera", cv2.WND_PROP_FULLSCREEN) if DEBUG is False: cv2.setWindowProperty( "Camera", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) task = RepeatedTimer(lapDuration, snap, folder, camera) shown = True # Display it cv2.imshow("Camera", captureImage(camera)) # Close window when pressing "esc" or "q" pressedKey = cv2.waitKey(1) & 0xFF if pressedKey == ord("q") or pressedKey == 27: break cv2.destroyAllWindows() task.stop()
def _stopAllTasks(self): """ Request that all running tasks stop """ # Loop through all running tasks for key in self.tasks.keys(): task = self.tasks.pop(key) task.stop()
def start_backup_scheduler(): logger.info("Starting backup scheduler") config = get_config() backup_schedule = config.get('Backup', 'schedule') if backup_schedule is None: # By defualt, we will do 2 backups. One in a random time between # 9-11 or 14-17 and another one 12 hours after that. # We are using 2, 3 and 4 because they will be summed with 12 bellow hour = random.choice([2, 3, 4, 9, 10]) minute = random.randint(0, 59) backup_schedule = '%d:%d,%s:%d' % (hour, minute, hour + 12, minute) config.set('Backup', 'schedule', backup_schedule) config.flush() backup_hours = [map(int, i.strip().split(':')) for i in backup_schedule.split(',')] def _backup_task(): for i in xrange(3): # FIXME: This is SO UGLY, we should be calling backup_database # task directly, but duplicity messes with multiprocessing in a # way that it will not work args = sys.argv[:] for i, arg in enumerate(args[:]): if arg == 'run': args[i] = 'backup_database' break p = subprocess.Popen(args) stdout, stderr = p.communicate() if p.returncode == 0: break else: logger.warning( "Failed to backup database:\nstdout: %s\nstderr: %s", stdout, stderr) # Retry again with a exponential backoff time.sleep((60 * 2) ** (i + 1)) for backup_hour in backup_hours: now = datetime.datetime.now() backup_date = now.replace(hour=backup_hour[0], minute=backup_hour[1], second=0, microsecond=0) if backup_date < now: backup_date = backup_date + datetime.timedelta(1) delta = backup_date - now backup_task = task.LoopingCall(_backup_task) # Schedule the task to start at the backup hour and repeat # every 24 hours reactor.callWhenRunning(reactor.callLater, delta.seconds, backup_task.start, 24 * 60 * 60) reactor.addSystemEventTrigger( 'before', 'shutdown', lambda: getattr(task, 'running', False) and task.stop())
def unload(self, shutdown=False): for task in self.repeated_tasks: try: task.stop() except AssertionError: continue
def onClose(self, clean, code, reason): log.msg("Disconnected") for task in (self.ping_task, self.pong_task): if task.running: task.stop() self.evt.emit('disconnected')