evennia.scripts.tickerhandler

TickerHandler

This implements an efficient Ticker which uses a subscription model to ‘tick’ subscribed objects at regular intervals.

The ticker mechanism is used by importing and accessing the instantiated TICKER_HANDLER instance in this module. This instance is run by the server; it will save its status across server reloads and be started automaticall on boot.

Example:

from evennia.scripts.tickerhandler import TICKER_HANDLER

# call tick myobj.at_tick(*args, **kwargs) every 15 seconds
TICKER_HANDLER.add(15, myobj.at_tick, *args, **kwargs)

You supply the interval to tick and a callable to call regularly with any extra args/kwargs. The callable should either be a stand-alone function in a module or the method on a typeclassed entity (that is, on an object that can be safely and stably returned from the database). Functions that are dynamically created or sits on in-memory objects cannot be used by the tickerhandler (there is no way to reference them safely across reboots and saves).

The handler will transparently set up and add new timers behind the scenes to tick at given intervals, using a TickerPool - all callables with the same interval will share the interval ticker.

To remove:

TICKER_HANDLER.remove(15, myobj.at_tick)

Both interval and callable must be given since a single object can be subscribed to many different tickers at the same time. You can also supply idstring as an identifying string if you ever want to tick the callable at the same interval but with different arguments (args/kwargs are not used for identifying the ticker). There is also persistent=False if you don’t want to make a ticker that don’t survive a reload. If either or both idstring or persistent has been changed from their defaults, they must be supplied to the TICKER_HANDLER.remove call to properly identify the ticker to remove.

The TickerHandler’s functionality can be overloaded by modifying the Ticker class and then changing TickerPool and TickerHandler to use the custom classes

class MyTicker(Ticker):
    # [doing custom stuff]

class MyTickerPool(TickerPool):
    ticker_class = MyTicker
class MyTickerHandler(TickerHandler):
    ticker_pool_class = MyTickerPool

If one wants to duplicate TICKER_HANDLER’s auto-saving feature in a custom handler one can make a custom AT_STARTSTOP_MODULE entry to call the handler’s save() and restore() methods when the server reboots.

class evennia.scripts.tickerhandler.Ticker(interval)[源代码]

基类:object

Represents a repeatedly running task that calls hooks repeatedly. Overload _callback to change the way it operates.

__init__(interval)[源代码]

Set up the ticker

参数

interval (int) – The stepping interval.

validate(start_delay=None)[源代码]

Start/stop the task depending on how many subscribers we have using it.

参数

start_delay (int, optional) – Time to way before starting.

add(store_key, *args, **kwargs)[源代码]

Sign up a subscriber to this ticker. :param store_key: Unique storage hash for this ticker subscription. :type store_key: str :param args: Arguments to call the hook method with. :type args: any, optional

关键字参数

_start_delay (int) – If set, this will be used to delay the start of the trigger instead of interval.

remove(store_key)[源代码]

Unsubscribe object from this ticker

参数

store_key (str) – Unique store key.

stop()[源代码]

Kill the Task, regardless of subscriptions.

class evennia.scripts.tickerhandler.TickerPool[源代码]

基类:object

This maintains a pool of evennia.scripts.scripts.ExtendedLoopingCall tasks for calling subscribed objects at given times.

ticker_class

Ticker 的别名

__init__()[源代码]

Initialize the pool.

add(store_key, *args, **kwargs)[源代码]

Add new ticker subscriber.

参数
  • store_key (str) – Unique storage hash.

  • args (any, optional) – Arguments to send to the hook method.

remove(store_key)[源代码]

Remove subscription from pool.

参数

store_key (str) – Unique storage hash to remove

stop(interval=None)[源代码]

Stop all scripts in pool. This is done at server reload since restoring the pool will automatically re-populate the pool.

参数

interval (int, optional) – Only stop tickers with this interval.

class evennia.scripts.tickerhandler.TickerHandler(save_name='ticker_storage')[源代码]

基类:object

The Tickerhandler maintains a pool of tasks for subscribing objects to various tick rates. The pool maintains creation instructions and and re-applies them at a server restart.

ticker_pool_class

TickerPool 的别名

__init__(save_name='ticker_storage')[源代码]

Initialize handler

save_name (str, optional): The name of the ServerConfig

instance to store the handler state persistently.

save()[源代码]

Save ticker_storage as a serialized string into a temporary ServerConf field. Whereas saving is done on the fly, if called by server when it shuts down, the current timer of each ticker will be saved so it can start over from that point.

restore(server_reload=True)[源代码]

Restore ticker_storage from database and re-initialize the handler from storage. This is triggered by the server at restart.

参数

server_reload (bool, optional) – If this is False, it means the server went through a cold reboot and all non-persistent tickers must be killed.

add(interval=60, callback=None, idstring='', persistent=True, *args, **kwargs)[源代码]

Add subscription to tickerhandler

参数

*args – Will be passed into the callback every time it’s called. This must be data possible to pickle.

关键字参数
  • interval (int) – Interval in seconds between calling callable(*args, **kwargs)

  • callable (callable function or method) – This should either be a stand-alone function or a method on a typeclassed entity (that is, one that can be saved to the database).

  • idstring (str) – Identifier for separating this ticker-subscription from others with the same interval. Allows for managing multiple calls with the same time interval and callback.

  • persistent (bool) – A ticker will always survive a server reload. If this is unset, the ticker will be deleted by a server shutdown.

  • Will be passed into the callback every time it is called. (**kwargs) – This must be data possible to pickle.

返回

store_key (tuple)

The immutable store-key for this ticker. This can

be stored and passed into .remove(store_key=store_key) later to easily stop this ticker later.

提示

The callback will be identified by type and stored either as as combination of serialized database object + methodname or as a python-path to the module + funcname. These strings will be combined iwth interval and idstring to define a unique storage key for saving. These must thus all be supplied when wanting to modify/remove the ticker later.

remove(interval=60, callback=None, idstring='', persistent=True, store_key=None)[源代码]

Remove ticker subscription from handler.

关键字参数
  • interval (int) – Interval of ticker to remove.

  • callback (callable function or method) – Either a function or the method of a typeclassed object.

  • idstring (str) – Identifier id of ticker to remove.

  • persistent (bool) – Whether this ticker is persistent or not.

  • store_key (str) – If given, all other kwargs are ignored and only this is used to identify the ticker.

引发

KeyError – If no matching ticker was found to remove.

提示

The store-key is normally built from the interval/callback/idstring/persistent values; but if the store_key is explicitly given, this is used instead.

clear(interval=None)[源代码]

Stop/remove tickers from handler.

参数

interval (int, optional) – Only stop tickers with this interval.

提示

This is the only supported way to kill tickers related to non-db objects.

all(interval=None)[源代码]

Get all ticker subscriptions.

参数

interval (int, optional) – Limit match to tickers with this interval.

返回

list or dict – If interval was given, this is a list of tickers using that interval. If interval was not given, this is a dict {interval1: [ticker1, ticker2, …], …}

all_display()[源代码]

Get all tickers on an easily displayable form.

返回

tickers (dict) – A list of all storekeys