async def mainloop(): msg_queue = curio.UniversalQueue() quit_event = curio.UniversalEvent() parent = "NextTarget" uploader = FailSafeUploader(msg_queue, quit_event, get_firebase_default) uploader_thread = threading.Thread(target=uploader.upload, args=(parent, )) uploader_thread.start() zmqloop = await curio.spawn(loop_zmq(msg_queue)) goodbye = curio.SignalEvent(signal.SIGINT, signal.SIGTERM) await goodbye.wait() print("stopping fb uploader") await quit_event.set() await msg_queue.put(dict(_task="quit")) await curio.run_in_thread(uploader_thread.join) try: await zmqloop.cancel() except: import traceback traceback.print_exc()
async def parent(): """ The parent is (somewhat) patiently waiting for the kid. The parent cancels the kid task after waiting 10 seconds. The parent makes the kids wait five seconds before allowing them to start playing. 9. Capture an external signal before telling the kids to stop. :return: """ goodbye = curio.SignalEvent(signal.SIGINT, signal.SIGTERM) kid_task = await curio.spawn(kid) await curio.sleep(5) print('Yes, go play.') await start_evt.set() # await curio.sleep(5) await goodbye.wait() print("Let's go") count_task = await curio.spawn(countdown, 10) await count_task.join() print("We're leaving!") try: await curio.timeout_after(10, kid_task.join) except curio.TaskTimeout: print('I warned you!') await kid_task.cancel() print('Leaving!')
async def serve(self, app: Application): Goodbye = curio.SignalEvent(signal.SIGINT, signal.SIGTERM) await app.notify('startup') task = await curio.spawn(self.run, app) await self.ready.set() print('Trinket serving on {}:{}'.format(*self.sockaddr)) await Goodbye.wait() print('Server is shutting down.') await app.notify('shutdown') print('Please wait. The remaining tasks are being terminated.') await task.cancel() self.ready.clear()
async def wait(timeout=None): duration = 0.0 increment = 0.01 shutdown = curio.SignalEvent(signal.SIGINT, signal.SIGTERM) if timeout is None: shutdown.wait() else: while True: if shutdown.is_set(): return if duration > timeout: return await curio.sleep(increment) duration += increment
async def parent(): goodbye = curio.SignalEvent(signal.SIGINT) kid_task = await curio.spawn(kid) await curio.sleep(5) print('Yes, go play') await start_evt.set() await goodbye.wait() del goodbye print("Let's go") count_task = await curio.spawn(countdown, 10) await count_task.join() print("We're leaving!") try: await curio.timeout_after(10, kid_task.join) except curio.TaskTimeout: print('I warned you!') await kid_task.cancel() print('Leaving!')
async def mainloop(watchroot, db_parent): # to communicate with fb uploader # set up the firebase uploader parent = db_parent uploader = FailSafeUploader(msg_queue, quit_event) uploader_thread = threading.Thread(target=uploader.upload, args=(parent, )) uploader_thread.start() curio_watcher_spawner = await curio.spawn(watcher_spawner()) dirs = [root for root, dirs, files in os.walk(watchroot)] for watchdir in dirs: logger.info('Start watching: {}'.format(watchdir)) await watchdir_queue.put(watchdir) # await tg.spawn(watch_continously(watchdir)) # tasks.append(t) goodbye = curio.SignalEvent(signal.SIGINT, signal.SIGTERM) await goodbye.wait() print("stopping fb uploader") await quit_event.set() await msg_queue.put(dict(_task="quit")) await curio.run_in_thread(uploader_thread.join) try: await curio_watcher_spawner.cancel() logger.info('Quit.') except: import traceback traceback.print_exc()
async def run(self) -> None: async with curio.TaskGroup() as g: cancel = curio.SignalEvent(signal.SIGINT, signal.SIGTERM) await g.spawn(self.dispatcher) if self.report_status: await g.spawn(self.status_reporter) if self.write_results: await g.spawn(self.database_writer) await g.spawn(self.broadcaster) for reporter in self.reporters: await g.spawn(reporter, self.readings) await cancel.wait() del cancel print('Shutting down server...', file=sys.stderr) await g.cancel_remaining() self.sensor_db.end_session()
async def _run(): self._sigevt = curio.SignalEvent(*args, **kwargs)