From 168bbdbaa7ff15f0d8d88e4411996c36d251b735 Mon Sep 17 00:00:00 2001 From: "s.kovbasa" Date: Tue, 19 Nov 2024 18:11:09 +0200 Subject: [PATCH] Drop loop parameter from AsyncIOExecutor altogether --- docs/changelog/changes_08.rst | 2 ++ hiku/executors/asyncio.py | 11 +++-------- tests/test_executor_asyncio.py | 10 ++++------ 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/docs/changelog/changes_08.rst b/docs/changelog/changes_08.rst index 34d1bd5..9fc5e13 100644 --- a/docs/changelog/changes_08.rst +++ b/docs/changelog/changes_08.rst @@ -40,6 +40,8 @@ Changes in 0.8 } } +- Drop ``loop`` parameter from ``hiku.executors.asyncio.AsyncIOExecutor`` constructor. + Backward-incompatible changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/hiku/executors/asyncio.py b/hiku/executors/asyncio.py index 450dc7b..bf48c10 100644 --- a/hiku/executors/asyncio.py +++ b/hiku/executors/asyncio.py @@ -1,14 +1,13 @@ import inspect from asyncio import ( FIRST_COMPLETED, - AbstractEventLoop, CancelledError, Task, gather, get_running_loop, wait, ) -from typing import TYPE_CHECKING, Any, Callable, Coroutine, Optional, cast +from typing import TYPE_CHECKING, Any, Callable, Coroutine, cast from hiku.executors.base import BaseAsyncExecutor from hiku.result import Proxy @@ -23,15 +22,11 @@ class AsyncIOExecutor(BaseAsyncExecutor): By default it allows to run both synchronous and asynchronous tasks. To deny synchronous tasks set deny_sync to True. - :param loop: asyncio event loop :param deny_sync: deny synchronous tasks - raise TypeError if a task is not awaitable """ - def __init__( - self, loop: Optional[AbstractEventLoop] = None, deny_sync: bool = False - ) -> None: - self.loop = loop + def __init__(self, deny_sync: bool = False) -> None: self.deny_sync = deny_sync async def _wrapper(self, fn: Callable, *args: Any, **kwargs: Any) -> Any: @@ -42,7 +37,7 @@ async def _wrapper(self, fn: Callable, *args: Any, **kwargs: Any) -> Any: return result def submit(self, fn: Callable, *args: Any, **kwargs: Any) -> Task: - loop = self.loop or get_running_loop() + loop = get_running_loop() coro = fn(*args, **kwargs) if not inspect.isawaitable(coro): diff --git a/tests/test_executor_asyncio.py b/tests/test_executor_asyncio.py index 6d2d692..1dff56f 100644 --- a/tests/test_executor_asyncio.py +++ b/tests/test_executor_asyncio.py @@ -2,8 +2,8 @@ import pytest -from hiku.executors.queue import Queue, Workflow from hiku.executors.asyncio import AsyncIOExecutor +from hiku.executors.queue import Queue, Workflow def func(): @@ -28,8 +28,7 @@ async def coroutine(): @pytest.mark.asyncio async def test_awaitable_check__async_only(): - loop = asyncio.get_running_loop() - executor = AsyncIOExecutor(loop, deny_sync=True) + executor = AsyncIOExecutor(deny_sync=True) with pytest.raises(TypeError) as func_err: executor.submit(func) @@ -52,8 +51,7 @@ async def test_awaitable_check__async_only(): @pytest.mark.asyncio async def test_awaitable_check__sync_async(): - loop = asyncio.get_running_loop() - executor = AsyncIOExecutor(loop) + executor = AsyncIOExecutor() assert await executor.submit(func) is None assert await executor.submit(func2) == [] @@ -79,7 +77,7 @@ class TestWorkflow(Workflow): def result(self): raise AssertionError("impossible") - executor = AsyncIOExecutor(loop) + executor = AsyncIOExecutor() queue = Queue(executor) queue.submit(queue.fork(None), proc)