117 lines
1.8 KiB
Markdown
117 lines
1.8 KiB
Markdown
|
+++
|
||
|
title = "Timer"
|
||
|
weight = 7
|
||
|
+++
|
||
|
|
||
|
El `timer` siempre se ejecuta en otro hilo.
|
||
|
|
||
|
### once
|
||
|
|
||
|
Ejecutar macro una sola vez en X segundos.
|
||
|
|
||
|
|
||
|
```python
|
||
|
import easymacro as app
|
||
|
|
||
|
|
||
|
NOMBRE = 'reloj'
|
||
|
|
||
|
|
||
|
def mostrar_hora():
|
||
|
app.debug(app.dates.now_time)
|
||
|
return
|
||
|
|
||
|
|
||
|
def iniciar_conteo():
|
||
|
segundos = 5
|
||
|
macro = {
|
||
|
'library': 'test',
|
||
|
'name': 'mostrar_hora',
|
||
|
}
|
||
|
app.timer.once(NOMBRE, segundos, macro)
|
||
|
return
|
||
|
|
||
|
|
||
|
def main(args=None):
|
||
|
iniciar_conteo()
|
||
|
return
|
||
|
```
|
||
|
|
||
|
### cancel
|
||
|
|
||
|
Cancelar ejecución, antes del tiempo establecido.
|
||
|
|
||
|
```python
|
||
|
def main(args=None):
|
||
|
iniciar_conteo()
|
||
|
app.sleep(3)
|
||
|
detener_conteo()
|
||
|
return
|
||
|
|
||
|
def detener_conteo():
|
||
|
app.timer.cancel(NOMBRE)
|
||
|
return
|
||
|
```
|
||
|
|
||
|
```
|
||
|
16/08/2022 21:18:50 - INFO - Event: "reloj", started... execute in 60 seconds
|
||
|
16/08/2022 21:18:55 - INFO - Cancel event: "reloj", ok...
|
||
|
```
|
||
|
|
||
|
### start
|
||
|
|
||
|
Ejecutar macro cada X segundos.
|
||
|
|
||
|
```python
|
||
|
NOMBRE = 'reloj'
|
||
|
|
||
|
|
||
|
def mostrar_hora():
|
||
|
app.debug(app.dates.now_time)
|
||
|
return
|
||
|
|
||
|
|
||
|
def iniciar_reloj():
|
||
|
segundos = 1
|
||
|
macro = {
|
||
|
'library': 'test',
|
||
|
'name': 'mostrar_hora',
|
||
|
}
|
||
|
app.timer.start(NOMBRE, segundos, macro)
|
||
|
return
|
||
|
|
||
|
def main(args=None):
|
||
|
iniciar_reloj()
|
||
|
return
|
||
|
```
|
||
|
|
||
|
### stop
|
||
|
|
||
|
Detener timer.
|
||
|
|
||
|
```python
|
||
|
def detener_reloj():
|
||
|
app.timer.stop(NOMBRE)
|
||
|
return
|
||
|
```
|
||
|
|
||
|
```
|
||
|
16/08/2022 21:25:37 - INFO - Timer 'reloj' started, execute macro: 'mostrar_hora'
|
||
|
16/08/2022 21:25:38 - DEBUG - 21:25:38
|
||
|
16/08/2022 21:25:39 - DEBUG - 21:25:39
|
||
|
...
|
||
|
16/08/2022 21:25:47 - DEBUG - 21:25:47
|
||
|
16/08/2022 21:25:48 - DEBUG - 21:25:48
|
||
|
16/08/2022 21:25:48 - INFO - Timer stopped...
|
||
|
```
|
||
|
|
||
|
|
||
|
{{% notice tip %}}
|
||
|
Asegurese siempre de establecer un nombre único para cada timer.
|
||
|
{{% /notice %}}
|
||
|
|
||
|
|
||
|
{{% notice warning %}}
|
||
|
Asegurese siempre de ejecutar macros que NO bloqueen la interfaz del usuario.
|
||
|
{{% /notice %}}
|