morington

Untitled

Mar 29th, 2024
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import asyncio
  2. import time
  3. from typing import Any, Coroutine, Callable, TypeAlias
  4.  
  5. T: TypeAlias = Callable[..., Coroutine[Any, Any, Any]]
  6.  
  7.  
  8. class Preprocessor:
  9.     def __init__(self):
  10.         self.stopping_point_for_all_decorators = True
  11.  
  12.     @staticmethod
  13.     def launch_frequency(interval_seconds: int) -> Any:
  14.         def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
  15.             def wrapper(*args, **kwargs) -> Any:
  16.                 self = args[0]
  17.                 while self.stopping_point_for_all_decorators:
  18.                     func(*args, **kwargs)
  19.                     time.sleep(interval_seconds)
  20.  
  21.             return wrapper
  22.         return decorator
  23.  
  24.     @staticmethod
  25.     def async_launch_frequency(interval_seconds: int) -> Callable[[T], T]:
  26.         def decorator(func: T) -> T:
  27.             async def wrapper(*args: Any, **kwargs: Any) -> Any:
  28.                 self = args[0]
  29.                 while self.stopping_point_for_all_decorators:
  30.                     await func(*args, **kwargs)
  31.                     await asyncio.sleep(interval_seconds)
  32.  
  33.             return wrapper
  34.         return decorator
  35.  
  36.     @async_launch_frequency(1)
  37.     def test(self) -> None:
  38.         print('Test')
Advertisement
Add Comment
Please, Sign In to add comment