Skip to content

Commit

Permalink
timeout tokens summing optimization: the sum will be only token with …
Browse files Browse the repository at this point in the history
…near deadline
  • Loading branch information
pomponchik committed Aug 7, 2024
1 parent 5111766 commit ba632f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
27 changes: 16 additions & 11 deletions cantok/tokens/abstract/abstract_token.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sys import getrefcount
from abc import ABC, abstractmethod
from threading import RLock
from typing import List, Dict, Awaitable, Optional, Union, Any
from typing import Tuple, List, Dict, Awaitable, Optional, Union, Any


from cantok.errors import CancellationError
Expand All @@ -15,17 +15,9 @@ class AbstractToken(ABC):
rollback_if_nondirect_polling = False

def __init__(self, *tokens: 'AbstractToken', cancelled: bool = False) -> None:
from cantok import DefaultToken

self.cached_report: Optional[CancellationReport] = None
self._cancelled: bool = cancelled
self.tokens: List[AbstractToken] = []

for token in tokens:
if isinstance(token, DefaultToken):
pass
else:
self.tokens.append(token)
self.tokens: List[AbstractToken] = self.filter_tokens(tokens)

self.lock: RLock = RLock()

Expand Down Expand Up @@ -81,12 +73,25 @@ def __add__(self, item: 'AbstractToken') -> 'AbstractToken':
if container_token is None:
return SimpleToken(*nested_tokens)
else:
container_token.tokens.extend(nested_tokens)
container_token.tokens.extend(container_token.filter_tokens(nested_tokens))
return container_token

def __bool__(self) -> bool:
return self.keep_on()

def filter_tokens(self, tokens: Tuple['AbstractToken', ...]) -> List['AbstractToken']:
from cantok import DefaultToken

result: List[AbstractToken] = []

for token in tokens:
if isinstance(token, DefaultToken):
pass
else:
result.append(token)

return result

@property
def cancelled(self) -> bool:
return self.is_cancelled()
Expand Down
18 changes: 16 additions & 2 deletions cantok/tokens/timeout_token.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from time import monotonic_ns, perf_counter

from typing import Union, Callable, Dict, Any
from typing import Union, Callable, Tuple, List, Dict, Any

from cantok import AbstractToken
from cantok import ConditionToken
Expand All @@ -25,11 +25,25 @@ def __init__(self, timeout: Union[int, float], *tokens: AbstractToken, cancelled
timer = perf_counter

start_time: Union[int, float] = timer()
deadline = start_time + timeout
def function() -> bool:
return timer() >= (start_time + timeout)
return timer() >= deadline

self.deadline = deadline

super().__init__(function, *tokens, cancelled=cancelled)

def filter_tokens(self, tokens: Tuple['AbstractToken', ...]) -> List['AbstractToken']:
result: List[AbstractToken] = []

for token in tokens:
if isinstance(token, TimeoutToken) and token.monotonic == self.monotonic and self.deadline < token.deadline:
result.extend(token.tokens)
else:
result.append(token)

return super().filter_tokens(result)

def text_representation_of_superpower(self) -> str:
return str(self.timeout)

Expand Down

0 comments on commit ba632f8

Please sign in to comment.