Skip to content

Commit

Permalink
Fixed regression caused by the use of _get_running_loop()
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Jun 4, 2018
1 parent ff2448e commit 6ac3aa3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -42,7 +42,7 @@ jobs:
on:
tags: true

python: "3.5"
python: "3.5.2"

install:
- pip install "setuptools >= 36.2.7"
Expand Down
22 changes: 16 additions & 6 deletions asyncio_extras/threads.py
@@ -1,13 +1,22 @@
import concurrent.futures
import gc
import inspect
from asyncio import get_event_loop, _get_running_loop, Future, AbstractEventLoop
from asyncio import get_event_loop, Future, AbstractEventLoop
from concurrent.futures import Executor
from functools import wraps, partial
from inspect import isawaitable
from threading import Event
from typing import Optional, Callable, Union

try:
from asyncio import _get_running_loop
except ImportError:
def _get_running_loop():
try:
return get_event_loop()
except RuntimeError:
return None

__all__ = ('threadpool', 'call_in_executor', 'call_async')


Expand Down Expand Up @@ -53,13 +62,14 @@ def __aexit__(self, exc_type, exc_val, exc_tb):
def __call__(self, func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs):
loop = _get_running_loop()
if loop:
callback = partial(func, *args, **kwargs)
return loop.run_in_executor(self.executor, callback)
else:
try:
loop = get_event_loop()
except RuntimeError:
# Event loop not available -- we're in a worker thread
return func(*args, **kwargs)
else:
callback = partial(func, *args, **kwargs)
return loop.run_in_executor(self.executor, callback)

assert not inspect.iscoroutinefunction(func), \
'Cannot wrap coroutine functions to be run in an executor'
Expand Down
5 changes: 5 additions & 0 deletions docs/versionhistory.rst
Expand Up @@ -3,6 +3,11 @@ Version history

This library adheres to `Semantic Versioning <http://semver.org/>`_.

**1.3.2** (2018-06-04)

- Fixed regression on older Python 3.5 releases caused by the introduction of
``_get_running_loop()`` calls

**1.3.1** (2018-06-03)

- Fixed ``StopAsyncIteration`` exceptions from leaking from async context managers if ``return``
Expand Down

0 comments on commit 6ac3aa3

Please sign in to comment.