| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import asyncio |
| 4 |
from typing import Type, Union |
| 5 |
|
| 6 |
from ..typing import Event |
| 7 |
|
| 8 |
|
| 9 |
class EventWrapper: |
| 10 |
def __init__(self) -> None: |
| 11 |
self._event = asyncio.Event() |
| 12 |
|
| 13 |
async def clear(self) -> None: |
| 14 |
self._event.clear() |
| 15 |
|
| 16 |
async def wait(self) -> None: |
| 17 |
await self._event.wait() |
| 18 |
|
| 19 |
async def set(self) -> None: |
| 20 |
self._event.set() |
| 21 |
|
| 22 |
def is_set(self) -> bool: |
| 23 |
return self._event.is_set() |
| 24 |
|
| 25 |
|
| 26 |
class WorkerContext: |
| 27 |
event_class: Type[Event] = EventWrapper |
| 28 |
|
| 29 |
def __init__(self) -> None: |
| 30 |
self.terminated = self.event_class() |
| 31 |
|
| 32 |
@staticmethod |
| 33 |
async def sleep(wait: Union[float, int]) -> None: |
| 34 |
return await asyncio.sleep(wait) |
| 35 |
|
| 36 |
@staticmethod |
| 37 |
def time() -> float: |
| 38 |
return asyncio.get_event_loop().time() |