Skip to content
Snippets Groups Projects
Commit 386afa4c authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Timer and Hooks now accept func and coro functions

parent aa0b7365
No related branches found
No related tags found
1 merge request!1First try of type hints
......@@ -36,7 +36,7 @@ class HookType(Enum):
class Hook(object):
__slots__ = ['type', 'func', 'args', 'kwargs']
def __init__(self, type_:HookType, func: Any, *args, **kwargs):
def __init__(self, type_:HookType, func: core.FuncT, *args, **kwargs):
# func has to be a callable, but it can be a coroutine or a function
self.type = type_
self.func = func
......@@ -83,11 +83,11 @@ class AsyncEngine(core.EngineMixin):
#####################################################
# Hooks
#####################################################
def on_start(self, func: Any, *args, **kwargs):
def on_start(self, func: core.FuncT, *args, **kwargs):
hook = Hook(HookType.start, func, *args, **kwargs)
self._hooks.append(hook)
def on_stop(self, func: Any, *args, **kwargs):
def on_stop(self, func: core.FuncT, *args, **kwargs):
hook = Hook(HookType.stop, func, *args, **kwargs)
self._hooks.append(hook)
......
......@@ -22,7 +22,7 @@ import inspect
import logging
import time
import typing
from typing import Any, Optional, List, Callable
from typing import Any, Awaitable, Optional, List, Callable, Union, TypeVar
from .exceptions import EngineError, XAALError
from .messages import ALIVE_ADDR, MessageAction, MessageFactory, MessageType
......@@ -32,6 +32,7 @@ if typing.TYPE_CHECKING:
from .messages import Message
FuncT = TypeVar("FuncT", Callable[[], None], Callable[[], Awaitable[None]])
logger = logging.getLogger(__name__)
......@@ -39,7 +40,7 @@ logger = logging.getLogger(__name__)
# Timer class
#####################################################
class Timer(object):
def __init__(self, func: Any, period: int, counter: int):
def __init__(self, func: FuncT, period: int, counter: int):
# Timer function should a Callable[[],None], but it can be a coroutine too
self.func = func
self.period = period
......@@ -208,7 +209,8 @@ class EngineMixin(object):
#####################################################
# timers
#####################################################
def add_timer(self, func: Any, period: int, counter: int = -1):
from typing import Coroutine
def add_timer(self, func: FuncT, period: int, counter: int = -1):
"""
func: function to call
period: period in second
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment