Skip to content

BaseTimer - Пишем свой Таймер

Base timer class.

BaseTimer

Bases: Generic[TAsyncFlag], ABC

BaseTimer - An abstract class that is the basis for Timers.

Example

from qtasks import QueueTasks
from qtasks.timers.base import BaseTimer

class MyTimer(BaseTimer):
    def __init__(self, app: QueueTasks):
        super().__init__(app=app)
        pass
Source code in src/qtasks/timers/base.py
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
class BaseTimer(Generic[TAsyncFlag], ABC):
    """
    `BaseTimer` - An abstract class that is the basis for Timers.

    ## Example

    ```python
    from qtasks import QueueTasks
    from qtasks.timers.base import BaseTimer

    class MyTimer(BaseTimer):
        def __init__(self, app: QueueTasks):
            super().__init__(app=app)
            pass
    ```
    """

    def __init__(
        self,
        app: Annotated[
            Union[QueueTasks, aioQueueTasks],
            Doc("""
                    Task.

                    Default: `{qtasks.QueueTasks}` or `{qtasks.asyncio.QueueTasks}`.
                    """),
        ],
        log: Annotated[
            Logger | None,
            Doc("""
                    Logger.

                    Default: `qtasks.logs.Logger`.
                    """),
        ] = None,
        config: Annotated[
            QueueConfig | None,
            Doc("""
                    Config.

                    Default: `qtasks.configs.config.QueueConfig`.
                    """),
        ] = None,
    ):
        """
        Timer initialization.

        Args:
            app (QueueTasks): Application.
            log (Logger, optional): Logger. Default: `qtasks.logs.Logger`.
            config (QueueConfig, optional): Config. Default: `qtasks.configs.config.QueueConfig`.
        """
        self.app = app
        self.config = config or self.app.config
        self.log = (
            log.with_subname("Timer")
            if log
            else Logger(
                name=self.app.name,
                subname="Timer",
                default_level=self.config.logs_default_level_server,
                format=self.config.logs_format,
            )
        )

    @overload
    def add_task(
        self: BaseTimer[Literal[False]],
        *args: Annotated[
            Any,
            Doc("""
                    args of the task.

                    Default: `()`.
                    """),
        ],
        task_name: Annotated[
            str,
            Doc("""
                    Task name.
                    """),
        ],
        priority: Annotated[
            int | None,
            Doc("""
                    The task has priority.

                    Default: Task priority value.
                    """),
        ] = None,
        timeout: Annotated[
            float | None,
            Doc("""
                    Task timeout.

                    If specified, the task is returned via `qtasks.results.AsyncTask`.
                    """),
        ] = None,
        trigger: Annotated[
            Any,
            Doc("""
                    Task trigger.
                    """),
        ],
        **kwargs: Annotated[
            Any,
            Doc("""
                    kwargs tasks.

                    Default: `{}`.
                    """),
        ],
    ) -> Any | None: ...

    @overload
    async def add_task(
        self: BaseTimer[Literal[True]],
        *args: Annotated[
            Any,
            Doc("""
                    args of the task.

                    Default: `()`.
                    """),
        ],
        task_name: Annotated[
            str,
            Doc("""
                    Task name.
                    """),
        ],
        priority: Annotated[
            int | None,
            Doc("""
                    The task has priority.

                    Default: Task priority value.
                    """),
        ] = None,
        timeout: Annotated[
            float | None,
            Doc("""
                    Task timeout.

                    If specified, the task is returned via `qtasks.results.AsyncTask`.
                    """),
        ] = None,
        trigger: Annotated[
            Any,
            Doc("""
                    Task trigger.
                    """),
        ],
        **kwargs: Annotated[
            Any,
            Doc("""
                    kwargs tasks.

                    Default: `{}`.
                    """),
        ],
    ) -> Any | None: ...

    @abstractmethod
    def add_task(
        self,
        *args: Annotated[
            Any,
            Doc("""
                    args of the task.

                    Default: `()`.
                    """),
        ],
        task_name: Annotated[
            str,
            Doc("""
                    Task name.
                    """),
        ],
        priority: Annotated[
            int | None,
            Doc("""
                    The task has priority.

                    Default: Task priority value.
                    """),
        ] = None,
        timeout: Annotated[
            float | None,
            Doc("""
                    Task timeout.

                    If specified, the task is returned via `qtasks.results.AsyncTask`.
                    """),
        ] = None,
        trigger: Annotated[
            Any,
            Doc("""
                    Task trigger.
                    """),
        ],
        **kwargs: Annotated[
            Any,
            Doc("""
                    kwargs tasks.

                    Default: `{}`.
                    """),
        ],
    ) -> Any | None | Awaitable[Any | None]:
        """
        Adding a task.

        Args:
            task_name (str): The name of the task.
            trigger (Any, optional): Trigger values.
            priority (int, optional): Task priority. Default is `0`.
            args (tuple, optional): task args. Defaults to `()`.
            kwargs (dict, optional): kwags tasks. Defaults to `{}`.

        Returns:
            Any|None: Task.
        """
        pass

    def run_forever(self):
        """Start Timer."""
        pass

__init__(app, log=None, config=None)

Timer initialization.

Parameters:

Name Type Description Default
app QueueTasks

Application.

required
log Logger

Logger. Default: qtasks.logs.Logger.

None
config QueueConfig

Config. Default: qtasks.configs.config.QueueConfig.

None
Source code in src/qtasks/timers/base.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def __init__(
    self,
    app: Annotated[
        Union[QueueTasks, aioQueueTasks],
        Doc("""
                Task.

                Default: `{qtasks.QueueTasks}` or `{qtasks.asyncio.QueueTasks}`.
                """),
    ],
    log: Annotated[
        Logger | None,
        Doc("""
                Logger.

                Default: `qtasks.logs.Logger`.
                """),
    ] = None,
    config: Annotated[
        QueueConfig | None,
        Doc("""
                Config.

                Default: `qtasks.configs.config.QueueConfig`.
                """),
    ] = None,
):
    """
    Timer initialization.

    Args:
        app (QueueTasks): Application.
        log (Logger, optional): Logger. Default: `qtasks.logs.Logger`.
        config (QueueConfig, optional): Config. Default: `qtasks.configs.config.QueueConfig`.
    """
    self.app = app
    self.config = config or self.app.config
    self.log = (
        log.with_subname("Timer")
        if log
        else Logger(
            name=self.app.name,
            subname="Timer",
            default_level=self.config.logs_default_level_server,
            format=self.config.logs_format,
        )
    )

add_task(*args, task_name, priority=None, timeout=None, trigger, **kwargs) abstractmethod

add_task(*args: Annotated[Any, Doc('\n                    args of the task.\n\n                    Default: `()`.\n                    ')], task_name: Annotated[str, Doc('\n                    Task name.\n                    ')], priority: Annotated[int | None, Doc('\n                    The task has priority.\n\n                    Default: Task priority value.\n                    ')] = None, timeout: Annotated[float | None, Doc('\n                    Task timeout.\n\n                    If specified, the task is returned via `qtasks.results.AsyncTask`.\n                    ')] = None, trigger: Annotated[Any, Doc('\n                    Task trigger.\n                    ')], **kwargs: Annotated[Any, Doc('\n                    kwargs tasks.\n\n                    Default: `{}`.\n                    ')]) -> Any | None
add_task(*args: Annotated[Any, Doc('\n                    args of the task.\n\n                    Default: `()`.\n                    ')], task_name: Annotated[str, Doc('\n                    Task name.\n                    ')], priority: Annotated[int | None, Doc('\n                    The task has priority.\n\n                    Default: Task priority value.\n                    ')] = None, timeout: Annotated[float | None, Doc('\n                    Task timeout.\n\n                    If specified, the task is returned via `qtasks.results.AsyncTask`.\n                    ')] = None, trigger: Annotated[Any, Doc('\n                    Task trigger.\n                    ')], **kwargs: Annotated[Any, Doc('\n                    kwargs tasks.\n\n                    Default: `{}`.\n                    ')]) -> Any | None

Adding a task.

Parameters:

Name Type Description Default
task_name str

The name of the task.

required
trigger Any

Trigger values.

required
priority int

Task priority. Default is 0.

None
args tuple

task args. Defaults to ().

()
kwargs dict

kwags tasks. Defaults to {}.

{}

Returns:

Type Description
Any | None | Awaitable[Any | None]

Any|None: Task.

Source code in src/qtasks/timers/base.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
@abstractmethod
def add_task(
    self,
    *args: Annotated[
        Any,
        Doc("""
                args of the task.

                Default: `()`.
                """),
    ],
    task_name: Annotated[
        str,
        Doc("""
                Task name.
                """),
    ],
    priority: Annotated[
        int | None,
        Doc("""
                The task has priority.

                Default: Task priority value.
                """),
    ] = None,
    timeout: Annotated[
        float | None,
        Doc("""
                Task timeout.

                If specified, the task is returned via `qtasks.results.AsyncTask`.
                """),
    ] = None,
    trigger: Annotated[
        Any,
        Doc("""
                Task trigger.
                """),
    ],
    **kwargs: Annotated[
        Any,
        Doc("""
                kwargs tasks.

                Default: `{}`.
                """),
    ],
) -> Any | None | Awaitable[Any | None]:
    """
    Adding a task.

    Args:
        task_name (str): The name of the task.
        trigger (Any, optional): Trigger values.
        priority (int, optional): Task priority. Default is `0`.
        args (tuple, optional): task args. Defaults to `()`.
        kwargs (dict, optional): kwags tasks. Defaults to `{}`.

    Returns:
        Any|None: Task.
    """
    pass

run_forever()

Start Timer.

Source code in src/qtasks/timers/base.py
253
254
255
def run_forever(self):
    """Start Timer."""
    pass