From 80a26aa494447000cb3a5ebef12e4853e8a43350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu> Date: Fri, 6 Jan 2017 11:17:07 +0100 Subject: [PATCH] Worker: report on asyncio tasks when shutting down. Only logged at debug level. Copied code from the Blender Cloud Add-on, where it has also been proven useful. --- .../flamenco_worker/cli.py | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/flamenco-worker-python/flamenco_worker/cli.py b/packages/flamenco-worker-python/flamenco_worker/cli.py index f1d09b1d..7ecce0e3 100644 --- a/packages/flamenco-worker-python/flamenco_worker/cli.py +++ b/packages/flamenco-worker-python/flamenco_worker/cli.py @@ -102,13 +102,45 @@ def main(): async def stop_loop(): log.info('Waiting to give tasks the time to stop gracefully') - await asyncio.sleep(2) + await asyncio.sleep(1) loop.stop() loop.run_until_complete(stop_loop()) except: log.exception('Uncaught exception!') - log.warning('Shutting down') + + log.warning('Closing asyncio loop') + + # Report on the asyncio task status + all_tasks = asyncio.Task.all_tasks() + if not len(all_tasks): + log.debug('no more scheduled tasks, this is a clean shutdown.') + elif all(task.done() for task in all_tasks): + log.debug('all %i tasks are done, this is a clean shutdown.', len(all_tasks)) + + import gc + import traceback + + # Clean up circular references between tasks. + gc.collect() + + for task_idx, task in enumerate(all_tasks): + if not task.done(): + continue + + # noinspection PyBroadException + try: + res = task.result() + log.debug(' task #%i: %s result=%r', task_idx, task, res) + except asyncio.CancelledError: + # No problem, we want to stop anyway. + log.debug(' task #%i: %s cancelled', task_idx, task) + except Exception: + print('{}: resulted in exception'.format(task)) + traceback.print_exc() + # for ref in gc.get_referrers(task): + # log.debug(' - referred by %s', ref) + loop.close() -- GitLab