Async exception handling (#3731)
* remove unused exception * add logging * disable pylint broad-except * add exception handler * fix lint * update log output * change log message in async with exc_info * Add exc_info to asyncio exception handler
This commit is contained in:
1 parent
1c24018fbb
commit
f1e5d32ef5
2 files changed
+30
-2
No files matched your search
@@ -1,6 +1,7 @@
|
||||
"""Asyncio backports for Python 3.4.3 compatibility."""
|
||||
import concurrent.futures
|
||||
import threading
|
||||
import logging
|
||||
from asyncio import coroutines
|
||||
from asyncio.futures import Future
|
||||
|
||||
@@ -13,6 +14,9 @@ except ImportError:
|
||||
ensure_future = async
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _set_result_unless_cancelled(fut, result):
|
||||
"""Helper setting the result only if the future was not cancelled."""
|
||||
if fut.cancelled():
|
||||
@@ -111,10 +115,12 @@ def run_coroutine_threadsafe(coro, loop):
|
||||
try:
|
||||
# pylint: disable=deprecated-method
|
||||
_chain_future(ensure_future(coro, loop=loop), future)
|
||||
# pylint: disable=broad-except
|
||||
except Exception as exc:
|
||||
if future.set_running_or_notify_cancel():
|
||||
future.set_exception(exc)
|
||||
raise
|
||||
else:
|
||||
_LOGGER.warning("Exception on lost future: ", exc_info=True)
|
||||
|
||||
loop.call_soon_threadsafe(callback)
|
||||
return future
|
||||
@@ -158,10 +164,12 @@ def run_callback_threadsafe(loop, callback, *args):
|
||||
"""Run callback and store result."""
|
||||
try:
|
||||
future.set_result(callback(*args))
|
||||
# pylint: disable=broad-except
|
||||
except Exception as exc:
|
||||
if future.set_running_or_notify_cancel():
|
||||
future.set_exception(exc)
|
||||
raise
|
||||
else:
|
||||
_LOGGER.warning("Exception on lost future: ", exc_info=True)
|
||||
|
||||
loop.call_soon_threadsafe(run_callback)
|
||||
return future
|
||||
Reference in new issue
Block a user