socktools.daemon.cron_timer - cron-like timers for daemons

This module provides a cron-like timer intended for use in daemon processes. Essentially, it lets you schedule events for every X seconds.

A lot of emphasis is placed on trying to get timings as accurate as possible while still using greenlets instead of full size threads.

To use it, setup the CronTimer class and configure your timed events then in your main loop call start_timer() and end_timer() before and after your other code. If you do not call end_timer() your scheduled events will not run.

class socktools.daemon.cron_timer.CronTimer(scheduled_events={})[source]

Cron-like timer for daemon processes

This class implements the cron-like timer for daemon processes. Events are scheduled to occur at a set frequency by registering them along with the time delay between function invocations.

In other words, if you wanted to schedule a function to run every hour you’d use a time delay of 3600.

Keyword Arguments:
 scheduled_events (dict) – Maps time delays to functions
end_timer()[source]

Ends the main loop timer and runs scheduled events

This method should be called at the end of your main loop in order to run the scheduled events.

get_default_events()[source]

Override this to add your own default events

This method is provided for the use of child classes and simply returns an events dict mapping time delays to lists of functions.

The default implementation returns an empty dict.

Note that you should NOT put the same callback in 2 different delays.

Returns:maps time delays to lists of functions
Return type:dict
get_sorted_delays()[source]

Returns a sorted list of delays

This method sorts the current scheduled events and returns them in a list starting with smallest first.

Returns:list of time delays starting from the smallest first
Return type:list
start_timer()[source]

Starts the main loop timer

To keep the timing accurate the main loop iterations are timed and the actual delay is calculated taking the main loop time into account

update_events()[source]

Updates all internal state

Call this method after updating the scheduled_events dict