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."""
uwsgiconf.runtime.scheduling.register_timer(period: int, *, target: Union[str, int, uwsgiconf.runtime.signals.Signal] = None) → Union[Callable, bool]

Add timer.

Can be used as a decorator:

@register_timer(3)
def repeat():
    do()
Parameters:
  • period (int) – The interval (seconds) at which to raise the signal.
  • target

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

    Available targets:

    • 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 of 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
Raises:

ValueError – If unable to add timer.

uwsgiconf.runtime.scheduling.register_timer_rb(period: int, *, repeat: int = None, target: Union[str, int, uwsgiconf.runtime.signals.Signal] = None) → Union[Callable, bool]

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

@register_timer_rb(3)
def repeat():
    do()
Parameters:
  • period – The interval (seconds) at which the signal is raised.
  • repeat – How many times to send signal. Will stop after ther number is reached. Default: None - infinitely.
  • target

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

    Available targets:

    • 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 of 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
Raises:

ValueError – If unable to add timer.

uwsgiconf.runtime.scheduling.register_timer_ms(period: int, *, target: Union[str, int, uwsgiconf.runtime.signals.Signal] = None) → Union[Callable, bool]

Add a millisecond resolution timer.

@register_timer_ms(300)
def repeat():
    do()
Parameters:
  • period – The interval (milliseconds) at which the signal is raised.
  • target

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

    Available targets:

    • 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 of 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
Raises:

ValueError – If unable to add timer.

uwsgiconf.runtime.scheduling.register_cron(*, weekday: Union[str, int] = None, month: Union[str, int] = None, day: Union[str, int] = None, hour: Union[str, int] = None, minute: Union[str, int] = None, target: Union[str, int, uwsgiconf.runtime.signals.Signal] = None) → Union[Callable, bool]

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

@register_cron(hour=-3)  # Every 3 hours.
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.

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

Parameters:
  • weekday – Day of a the week number. Defaults to each. 0 - Sunday 1 - Monday 2 - Tuesday 3 - Wednesday 4 - Thursday 5 - Friday 6 - Saturday
  • month – Month number 1-12. Defaults to each.
  • day – Day of the month number 1-31. Defaults to each.
  • hour – Hour 0-23. Defaults to each.
  • minute – Minute 0-59. Defaults to each.
  • target

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

    Available targets:

    • 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 of 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
Raises:

ValueError – If unable to add cron rule.