tools: Wrap Task _do() calls in a generic exception handler

Signed-off-by: Charles Baynham <charles.baynham@npl.co.uk>
This commit is contained in:
Charles Baynham 2019-09-09 12:30:13 +01:00 committed by Sébastien Bourdeauducq
parent e50a6d5aaf
commit 0b1fb255a9
1 changed files with 10 additions and 1 deletions

View File

@ -240,7 +240,16 @@ async def asyncio_wait_or_cancel(fs, **kwargs):
class TaskObject:
def start(self):
self.task = asyncio.ensure_future(self._do())
async def log_exceptions(awaitable):
try:
return await awaitable()
except asyncio.CancelledError:
raise
except Exception:
logger.error("Unhandled exception in TaskObject task body", exc_info=True)
raise
self.task = asyncio.ensure_future(log_exceptions(self._do))
async def stop(self):
self.task.cancel()