Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for Job class to have an __await__ method #512

Closed
wants to merge 10 commits into from
3 changes: 3 additions & 0 deletions aiojobs/_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,6 @@ def _report_exception(self, exc: BaseException) -> None:
if self._source_traceback is not None:
context["source_traceback"] = self._source_traceback
self._scheduler.call_exception_handler(context)

def __await__(self) -> _T:
return self.wait().__await__()
20 changes: 20 additions & 0 deletions tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,23 @@ async def coro() -> None:
assert job.get_name() == "changed_name"
assert job._task is not None
assert job._task.get_name() == "changed_name"


async def test_awaitable_job(scheduler: Scheduler) -> None:
async def coro() -> int:
"""Dummy function with result"""
return 1

job = await scheduler.spawn(coro())
result = await job
assert result == 1


async def test_job_gathering(scheduler: Scheduler):
async def coro() -> int:
"""Dummy function with result"""
return 1

jobs = [scheduler.spawn(coro()) for _ in range(5)]
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
results = await asyncio.gather(*jobs)
assert results == [1, 1, 1, 1, 1]