Skip to content

Scheduling

from uwsgiconf.runtime.scheduling import register_timer_rb, register_cron

@register_timer_rb(10, repeat=2)
def repeat_twice():
    """This function will be called twice with 10 seconds interval
    (by default in in first available mule) using red-black tree based timer.

    """

@register_cron(day=-3, hour='10-18/2')
def do_something():
    """This will be run every 3rd day, from 10 till 18 every 2 hours."""

Functions

register_cron

1
2
3
4
5
6
7
8
9
register_cron(
    weekday: Strint = None,  # (1)!
    month: Strint = None,  # (2)!
    day: Strint = None,  # (3)!
    hour: Strint = None,  # (4)!
    minute: Strint = None,  # (5)!
    target: TypeTarget = None,  # (6)!
    checker: TaskChecker | None = None,  # (7)!
) -> TypeRegResult
  1. Day of the week number. Defaults to each. 0 - Sunday 1 - Monday 2 - Tuesday 3 - Wednesday 4 - Thursday 5 - Friday 6 - Saturday

  2. Month number 1-12. Defaults to each.

  3. Day of the month number 1-31. Defaults to each.

  4. Hour 0-23. Defaults to each.

  5. Minute 0-59. Defaults to each.

  6. Existing signal to raise or Signal Target to register signal implicitly.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    Available signal targets:
    
        * Mule / Farm / Signal object - run on a certain mule, farm or issue a signal.
        * ``workers``  - run the signal handler on all the workers
        * ``workerN`` - run the signal handler only on worker N
        * ``worker``/``worker0`` - run the signal handler on the first available worker
        * ``active-workers`` - run the signal handlers on all the active [non-cheaped] workers
    
        * ``mules`` - run the signal handler on all the mules
        * ``muleN`` - run the signal handler on mule N
        * ``mule``/``mule0`` - run the signal handler on the first available mule
    
        * ``spooler`` - run the signal on the first available spooler
        * ``farmN/farm_XXX``  - run the signal handler in the mule farm N or named XXX
    
  7. TaskChecker to be used for task execution requirements checking.

Adds cron. The interface to the uWSGI signal cron facility.

1
2
3
4
# Every 3 hours. Don't run if env var NO_RUN_TASK=1
@register_cron(hour=-3, checker=TaskChecker(env='NO_RUN_TASK'))
def repeat():
    do()

Note

Arguments work similarly to a standard crontab, but instead of "*", use -1, and instead of "/2", "/3", etc. use -2 and -3, etc.

Note

Periods - rules like hour='10-18/2' (from 10 till 18 every 2 hours) - are allowed, but they are emulated by uwsgiconf. Use strings to define periods.

1
2
Keep in mind, that your actual function will be wrapped into another one, which will check
whether it is time to call your function.

ValueError If unable to add cron rule.


register_timer

1
2
3
4
5
register_timer(
    period: int, 
    target: TypeTarget = None,  # (1)!
    checker: TaskChecker | None = None,  # (2)!
) -> TypeRegResult
  1. Existing signal to raise or Signal Target to register signal implicitly.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    Available signal targets:
    
        * Mule / Farm / Signal object - run on a certain mule, farm or issue a signal.
        * ``workers``  - run the signal handler on all the workers
        * ``workerN`` - run the signal handler only on worker N
        * ``worker``/``worker0`` - run the signal handler on the first available worker
        * ``active-workers`` - run the signal handlers on all the active [non-cheaped] workers
    
        * ``mules`` - run the signal handler on all the mules
        * ``muleN`` - run the signal handler on mule N
        * ``mule``/``mule0`` - run the signal handler on the first available mule
    
        * ``spooler`` - run the signal on the first available spooler
        * ``farmN/farm_XXX``  - run the signal handler in the mule farm N or named XXX
    
  2. TaskChecker to be used for task execution requirements checking.

Add timer.

Can be used as a decorator:

1
2
3
4
5
.. code-block:: python

    @register_timer(3)
    def repeat():
        do()

ValueError If unable to add timer.


register_timer_ms

1
2
3
4
5
register_timer_ms(
    period: int,  # (1)!
    target: TypeTarget = None,  # (2)!
    checker: TaskChecker | None = None,  # (3)!
) -> TypeRegResult
  1. The interval (milliseconds) at which the signal is raised.

  2. Existing signal to raise or Signal Target to register signal implicitly.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    Available signal targets:
    
        * Mule / Farm / Signal object - run on a certain mule, farm or issue a signal.
        * ``workers``  - run the signal handler on all the workers
        * ``workerN`` - run the signal handler only on worker N
        * ``worker``/``worker0`` - run the signal handler on the first available worker
        * ``active-workers`` - run the signal handlers on all the active [non-cheaped] workers
    
        * ``mules`` - run the signal handler on all the mules
        * ``muleN`` - run the signal handler on mule N
        * ``mule``/``mule0`` - run the signal handler on the first available mule
    
        * ``spooler`` - run the signal on the first available spooler
        * ``farmN/farm_XXX``  - run the signal handler in the mule farm N or named XXX
    
  3. TaskChecker to be used for task execution requirements checking.

Add a millisecond resolution timer.

1
2
3
4
5
.. code-block:: python

    @register_timer_ms(300)
    def repeat():
        do()

ValueError If unable to add timer.


register_timer_rb

1
2
3
4
5
6
register_timer_rb(
    period: int,  # (1)!
    repeat: int | None = None,  # (2)!
    target: TypeTarget = None,  # (3)!
    checker: TaskChecker | None = None,  # (4)!
) -> TypeRegResult
  1. The interval (seconds) at which the signal is raised.

  2. How many times to send signal. Will stop after ther number is reached. Default: None - infinitely.

  3. Existing signal to raise or Signal Target to register signal implicitly.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    Available signal targets:
    
        * Mule / Farm / Signal object - run on a certain mule, farm or issue a signal.
        * ``workers``  - run the signal handler on all the workers
        * ``workerN`` - run the signal handler only on worker N
        * ``worker``/``worker0`` - run the signal handler on the first available worker
        * ``active-workers`` - run the signal handlers on all the active [non-cheaped] workers
    
        * ``mules`` - run the signal handler on all the mules
        * ``muleN`` - run the signal handler on mule N
        * ``mule``/``mule0`` - run the signal handler on the first available mule
    
        * ``spooler`` - run the signal on the first available spooler
        * ``farmN/farm_XXX``  - run the signal handler in the mule farm N or named XXX
    
  4. TaskChecker to be used for task execution requirements checking.

Add a red-black timer (based on black-red tree).

1
2
3
4
5
.. code-block:: python

    @register_timer_rb(3)
    def repeat():
        do()

ValueError If unable to add timer.