Package horizons :: Module scheduler :: Class Scheduler
[hide private]
[frames] | no frames]

Class Scheduler

source code

"Class providing timed callbacks.
Master of time.

TODO:
- Refactor to use a data structure that is suitable for iteration (ticking) as well as
  searching/deleting by instance, possibly also by callback.
  Suggestion: { tick -> { instance -> [callback] }} (basically a k-d tree)


@param timer: Timer instance the schedular registers itself with.

Instance Methods [hide private]
 
__init__(self, timer) source code
 
end(self) source code
 
tick(self, tick_id)
Threads main loop
source code
 
before_ticking(self)
Called after game load and before game has started.
source code
 
_run_additional_jobs(self) source code
 
add_object(self, callback_obj, readd=False)
Adds a new CallbackObject instance to the callbacks list for the first time
source code
 
add_new_object(self, callback, class_instance, run_in=1, loops=1, loop_interval=None, finish_callback=None)
Creates a new CallbackObject instance and calls the self.add_object() function.
source code
 
rem_object(self, callback_obj)
Removes a CallbackObject from all callback lists
source code
 
rem_all_classinst_calls(self, class_instance)
Removes all callbacks from the scheduler that belong to the class instance class_inst.
source code
 
rem_call(self, instance, callback)
Removes all callbacks of 'instance' that are 'callback'
source code
 
get_classinst_calls(self, instance, callback=None)
Returns all CallbackObjects of instance.
source code
 
get_remaining_ticks(self, instance, callback, assert_present=True)
Returns in how many ticks a callback is executed.
source code
 
get_ticks(self, seconds)
Call propagated to time instance
source code
 
get_ticks_of_month(self) source code
Class Variables [hide private]
  log = logging.getLogger("scheduler")
  FIRST_TICK_ID = 0
Method Details [hide private]

__init__(self, timer)
(Constructor)

source code 
Parameters:
  • timer - Timer obj

tick(self, tick_id)

source code 

Threads main loop

Parameters:
  • tick_id - int id of the tick.

before_ticking(self)

source code 

Called after game load and before game has started. Callbacks with run_in=0 are used as generic "do this as soon as the current context is finished". If this is done during load, it is supposed to mean tick -1, since it does not belong to the first tick. This method simulates this.

add_object(self, callback_obj, readd=False)

source code 

Adds a new CallbackObject instance to the callbacks list for the first time

Parameters:
  • callback_obj - CallbackObject type object, containing all necessary information
  • readd - Whether this object is added another time (looped)

add_new_object(self, callback, class_instance, run_in=1, loops=1, loop_interval=None, finish_callback=None)

source code 

Creates a new CallbackObject instance and calls the self.add_object() function.

Parameters:
  • callback - lambda function callback, which is called run_in ticks.
  • class_instance - class instance the function belongs to.
  • run_in - int number of ticks after which the callback is called. Defaults to 1, run next tick.
  • loops - How often the callback is called. -1 = infinite times. Defaults to 1, run once.
  • loop_interval - Delay between subsequent loops in ticks. Defaults to run_in.

rem_object(self, callback_obj)

source code 

Removes a CallbackObject from all callback lists

Parameters:
  • callback_obj - CallbackObject to remove
Returns:
int, number of removed calls

rem_call(self, instance, callback)

source code 

Removes all callbacks of 'instance' that are 'callback'

Parameters:
  • instance - the instance that would execute the call
  • callback - the function to remove
Returns:
int, number of removed calls

get_classinst_calls(self, instance, callback=None)

source code 

Returns all CallbackObjects of instance. Optionally, a specific callback can be specified.

Parameters:
  • instance - the instance to execute the call
  • callback - None to get all calls of instance, else only calls that execute callback.
Returns:
dict, entries: { CallbackObject: remaining_ticks_to_executing }

get_remaining_ticks(self, instance, callback, assert_present=True)

source code 

Returns in how many ticks a callback is executed. You must specify 1 single call.

Parameters:
  • * - just like get_classinst_calls
  • assert_present - assert that there must be sucha call @return int or possbile None if not assert_present