Skip to content

Commit

Permalink
Fixed asyncio backend options not being used (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm authored Dec 9, 2023
1 parent b429c1a commit 84c1bb0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
- Fixed adjusting the total number of tokens in a ``CapacityLimiter`` on asyncio failing
to wake up tasks waiting to acquire the limiter in certain edge cases (fixed with help
from Egor Blagov)
- Fixed ``loop_factory`` and ``use_uvloop`` options not being used on the asyncio
backend (`#643 <https://github.com/agronholm/anyio/issues/643>`_)

**4.1.0**

Expand Down
14 changes: 9 additions & 5 deletions src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
get_running_loop,
sleep,
)
from asyncio import run as native_run
from asyncio.base_events import _run_until_complete_cb # type: ignore[attr-defined]
from collections import OrderedDict, deque
from collections.abc import AsyncIterator, Generator, Iterable
Expand Down Expand Up @@ -165,7 +164,7 @@ def run(self, coro: Coroutine[T_Retval], *, context=None) -> T_Retval:

if context is None:
context = self._context
task = self._loop.create_task(coro, context=context)
task = context.run(self._loop.create_task, coro)

if (
threading.current_thread() is threading.main_thread()
Expand Down Expand Up @@ -1950,9 +1949,14 @@ async def wrapper() -> T_Retval:
del _task_states[task]

debug = options.get("debug", False)
options.get("loop_factory", None)
options.get("use_uvloop", False)
return native_run(wrapper(), debug=debug)
loop_factory = options.get("loop_factory", None)
if loop_factory is None and options.get("use_uvloop", False):
import uvloop

loop_factory = uvloop.new_event_loop

with Runner(debug=debug, loop_factory=loop_factory) as runner:
return runner.run(wrapper())

@classmethod
def current_token(cls) -> object:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import math
from asyncio import get_running_loop
from unittest.mock import AsyncMock

import pytest
Expand Down Expand Up @@ -44,3 +45,32 @@ async def async_add(x: int, y: int) -> int:

result = run(asyncio.create_task, async_add(1, 2), backend="asyncio")
assert result == 3


class TestAsyncioOptions:
def test_debug(self) -> None:
async def main() -> bool:
return get_running_loop().get_debug()

debug = run(main, backend="asyncio", backend_options={"debug": True})
assert debug is True

def test_loop_factory(self) -> None:
async def main() -> type:
return type(get_running_loop())

uvloop = pytest.importorskip("uvloop", reason="uvloop not installed")
loop_class = run(
main,
backend="asyncio",
backend_options={"loop_factory": uvloop.new_event_loop},
)
assert issubclass(loop_class, uvloop.Loop)

def test_use_uvloop(self) -> None:
async def main() -> type:
return type(get_running_loop())

uvloop = pytest.importorskip("uvloop", reason="uvloop not installed")
loop_class = run(main, backend="asyncio", backend_options={"use_uvloop": True})
assert issubclass(loop_class, uvloop.Loop)

0 comments on commit 84c1bb0

Please sign in to comment.