-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
130 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# from inspect import iscoroutine | ||
from typing import cast | ||
|
||
from omnipy.api.protocols.private.compute.job import IsJobBase | ||
|
||
# import anyio | ||
|
||
|
||
class AutoAsyncJobBaseMixin: | ||
def __init__( | ||
self, | ||
*, | ||
auto_async: bool = True, | ||
): | ||
self._auto_async = auto_async | ||
|
||
@property | ||
def auto_async(self) -> str | None: | ||
return self._auto_async | ||
|
||
async def _call_job(self, *args: object, **kwargs: object) -> object: | ||
super_as_job_base = cast(IsJobBase, super()) | ||
res = await super_as_job_base._call_job(*args, **kwargs) | ||
print('res', res) | ||
# if self.auto_async: | ||
# if not iscoroutine(res): | ||
# return anyio.to_thread.run_sync(lambda: res) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from inspect import iscoroutine | ||
|
||
import pytest | ||
import pytest_cases as pc | ||
|
||
from omnipy import TaskTemplate | ||
|
||
from ..cases.raw.functions import async_sleep_random_time_func, sync_sleep_random_time_func | ||
|
||
|
||
@pc.parametrize('async_task', [False, True], ids=['sync_task', 'async_task']) | ||
@pytest.mark.anyio | ||
def test_synchronously_run_task_with_auto_async(async_task: bool) -> None: | ||
task_func = async_sleep_random_time_func if async_task else sync_sleep_random_time_func | ||
|
||
_assert_synchronizity_of_task_is_same_as_task_func(async_task, task_func) | ||
|
||
task_auto = TaskTemplate(auto_async=True)(task_func).apply() | ||
seconds_auto = task_auto() | ||
assert not iscoroutine(seconds_auto) | ||
assert seconds_auto <= 0.1 | ||
|
||
|
||
@pc.parametrize('async_task', [False, True], ids=['sync_task', 'async_task']) | ||
@pytest.mark.anyio | ||
async def test_asynchronously_run_task_with_auto_async(async_task: bool) -> None: | ||
task_func = async_sleep_random_time_func if async_task else sync_sleep_random_time_func | ||
|
||
_assert_synchronizity_of_task_is_same_as_task_func(async_task, task_func) | ||
|
||
task_auto = TaskTemplate(auto_async=True)(task_func).apply() | ||
seconds_auto = task_auto() | ||
assert iscoroutine(seconds_auto) | ||
assert await seconds_auto <= 0.1 | ||
|
||
|
||
def _assert_synchronizity_of_task_is_same_as_task_func(async_task, task_func): | ||
task_no_auto = TaskTemplate(auto_async=False)(task_func).apply() | ||
seconds_no_auto = task_no_auto() | ||
if async_task: | ||
assert iscoroutine(seconds_no_auto) | ||
else: | ||
assert not iscoroutine(seconds_no_auto) | ||
assert seconds_no_auto <= 0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters