asyncio: Support PEP 492. Issue #24017.

This commit is contained in:
Yury Selivanov 2015-05-11 22:27:25 -04:00
parent d7e19bb566
commit 1af2bf75a2
6 changed files with 116 additions and 27 deletions

View file

@ -191,8 +191,8 @@ class BaseEventLoop(events.AbstractEventLoop):
self._thread_id = None
self._clock_resolution = time.get_clock_info('monotonic').resolution
self._exception_handler = None
self._debug = (not sys.flags.ignore_environment
and bool(os.environ.get('PYTHONASYNCIODEBUG')))
self.set_debug((not sys.flags.ignore_environment
and bool(os.environ.get('PYTHONASYNCIODEBUG'))))
# In debug mode, if the execution of a callback or a step of a task
# exceed this duration in seconds, the slow callback/task is logged.
self.slow_callback_duration = 0.1
@ -360,13 +360,18 @@ class BaseEventLoop(events.AbstractEventLoop):
return
if self._debug:
logger.debug("Close %r", self)
self._closed = True
self._ready.clear()
self._scheduled.clear()
executor = self._default_executor
if executor is not None:
self._default_executor = None
executor.shutdown(wait=False)
try:
self._closed = True
self._ready.clear()
self._scheduled.clear()
executor = self._default_executor
if executor is not None:
self._default_executor = None
executor.shutdown(wait=False)
finally:
# It is important to unregister "sys.coroutine_wrapper"
# if it was registered.
self.set_debug(False)
def is_closed(self):
"""Returns True if the event loop was closed."""
@ -1199,3 +1204,27 @@ class BaseEventLoop(events.AbstractEventLoop):
def set_debug(self, enabled):
self._debug = enabled
wrapper = coroutines.debug_wrapper
try:
set_wrapper = sys.set_coroutine_wrapper
except AttributeError:
pass
else:
current_wrapper = sys.get_coroutine_wrapper()
if enabled:
if current_wrapper not in (None, wrapper):
warnings.warn(
"loop.set_debug(True): cannot set debug coroutine "
"wrapper; another wrapper is already set %r" %
current_wrapper, RuntimeWarning)
else:
set_wrapper(wrapper)
else:
if current_wrapper not in (None, wrapper):
warnings.warn(
"loop.set_debug(False): cannot unset debug coroutine "
"wrapper; another wrapper was set %r" %
current_wrapper, RuntimeWarning)
else:
set_wrapper(None)