Skip to content

Commit

Permalink
Used the dataclass features to implement hashing and equality compari…
Browse files Browse the repository at this point in the history
…sons for TaskHandle
  • Loading branch information
agronholm committed Apr 21, 2024
1 parent 862db12 commit 588ea7e
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/asphalt/core/_concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
logger = logging.getLogger(__name__)


@dataclass
@dataclass(unsafe_hash=True)
class TaskHandle:
"""
A representation of a task started from :class:`TaskFactory`.
Expand All @@ -40,12 +40,14 @@ class TaskHandle:
function supported that
"""

name: str
start_value: Any = field(init=False, repr=False)
name: str = field(hash=False, compare=False)
start_value: Any = field(init=False, repr=False, compare=False)
_cancel_scope: CancelScope = field(
init=False, default_factory=CancelScope, repr=False
)
_finished_event: Event = field(init=False, default_factory=Event, repr=False)
_finished_event: Event = field(
init=False, default_factory=Event, repr=False, compare=False
)

def cancel(self) -> None:
"""Schedule the task to be cancelled."""
Expand All @@ -55,15 +57,6 @@ async def wait_finished(self) -> None:
"""Wait until the task is finished."""
await self._finished_event.wait()

def __hash__(self) -> int:
return hash(self._cancel_scope)

def __eq__(self, other: object) -> bool:
if isinstance(other, TaskHandle):
return self._cancel_scope is other._cancel_scope

return NotImplemented


async def _run_background_task(
func: Callable[..., Coroutine[Any, Any, Any]],
Expand Down

0 comments on commit 588ea7e

Please sign in to comment.