evennia.scripts.taskhandler

Module containing the task handler for Evennia deferred tasks, persistent or not.

evennia.scripts.taskhandler.handle_error(*args, **kwargs)[源代码]

Handle errors within deferred objects.

class evennia.scripts.taskhandler.TaskHandlerTask(task_id)[源代码]

基类:object

An object to represent a single TaskHandler task.

Instance Attributes:

task_id (int): the global id for this task deferred (deferred): a reference to this task’s deferred

Property Attributes:

paused (bool): check if the deferred instance of a task has been paused. called(self): A task attribute to check if the deferred instance of a task has been called.

pause()[源代码]

Pause the callback of a task.

unpause()[源代码]

Process all callbacks made since pause() was called.

do_task()[源代码]

Execute the task (call its callback).

call()[源代码]

Call the callback of this task.

remove()[源代码]

Remove a task without executing it.

cancel()[源代码]

Stop a task from automatically executing.

active()[源代码]

Check if a task is active (has not been called yet).

exists()[源代码]

Check if a task exists.

get_id()[源代码]

Returns the global id for this task. For use with

__init__(task_id)[源代码]

Initialize self. See help(type(self)) for accurate signature.

get_deferred()[源代码]

Return the instance of the deferred the task id is using.

返回

bool or deferred

An instance of a deferred or False if there is no task with the id.

None is returned if there is no deferred affiliated with this id.

pause()[源代码]

Pause the callback of a task. To resume use TaskHandlerTask.unpause.

unpause()[源代码]

Unpause a task, run the task if it has passed delay time.

property paused

A task attribute to check if the deferred instance of a task has been paused.

This exists to mock usage of a twisted deferred object.

返回

bool or None

True if the task was properly paused. None if the task does not have

a deferred instance.

do_task()[源代码]

Execute the task (call its callback). If calling before timedelay, cancel the deferred instance affliated to this task. Remove the task from the dictionary of current tasks on a successful callback.

返回

bool or any – Set to False if the task does not exist in task handler. Otherwise it will be the return of the task’s callback.

call()[源代码]

Call the callback of a task. Leave the task unaffected otherwise. This does not use the task’s deferred instance. The only requirement is that the task exist in task handler.

返回

bool or any – Set to False if the task does not exist in task handler. Otherwise it will be the return of the task’s callback.

remove()[源代码]

Remove a task without executing it. Deletes the instance of the task’s deferred.

参数

task_id (int) – an existing task ID.

返回

bool – True if the removal completed successfully.

cancel()[源代码]

Stop a task from automatically executing. This will not remove the task.

返回

bool

True if the cancel completed successfully.

False if the cancel did not complete successfully.

active()[源代码]

Check if a task is active (has not been called yet).

返回

bool

True if a task is active (has not been called yet). False if

it is not (has been called) or if the task does not exist.

property called

A task attribute to check if the deferred instance of a task has been called.

This exists to mock usage of a twisted deferred object. It will not set to True if Task.call has been called. This only happens if task’s deferred instance calls the callback.

返回

bool

True if the deferred instance of this task has called the callback.

False if the deferred instnace of this task has not called the callback.

exists()[源代码]

Check if a task exists. Most task handler methods check for existence for you.

返回

bool – True the task exists False if it does not.

get_id()[源代码]

Returns the global id for this task. For use with evennia.scripts.taskhandler.TASK_HANDLER.

返回

task_id (int) – global task id for this task.

class evennia.scripts.taskhandler.TaskHandler[源代码]

基类:object

A light singleton wrapper allowing to access permanent tasks.

When utils.delay is called, the task handler is used to create the task.

Task handler will automatically remove uncalled but canceled from task handler. By default this will not occur until a canceled task has been uncalled for 60 second after the time it should have been called. To adjust this time use TASK_HANDLER.stale_timeout. If stale_timeout is 0 stale tasks will not be automatically removed. This is not done on a timer. I is done as new tasks are added or the load method is called.

__init__()[源代码]

Initialize self. See help(type(self)) for accurate signature.

load()[源代码]

Load from the ServerConfig.

This should be automatically called when Evennia starts. It populates self.tasks according to the ServerConfig.

clean_stale_tasks()[源代码]

remove uncalled but canceled from task handler.

By default this will not occur until a canceled task has been uncalled for 60 second after the time it should have been called. To adjust this time use TASK_HANDLER.stale_timeout.

save()[源代码]

Save the tasks in ServerConfig.

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

Add a new task.

If the persistent kwarg is truthy: The callback, args and values for kwarg will be serialized. Type and attribute errors during the serialization will be logged, but will not throw exceptions. For persistent tasks do not use memory references in the callback function or arguments. After a restart those memory references are no longer accurate.

参数
  • timedelay (int or float) – time in seconds before calling the callback.

  • callback (function or instance method) – the callback itself

  • any (any) – any additional positional arguments to send to the callback

  • *args – positional arguments to pass to callback.

  • **kwargs

    keyword arguments to pass to callback. - persistent (bool, optional): persist the task (stores it).

    Persistent key and value is removed from kwargs it will not be passed to callback.

返回

TaskHandlerTask

An object to represent a task.

Reference evennia.scripts.taskhandler.TaskHandlerTask for complete details.

exists(task_id)[源代码]

Check if a task exists. Most task handler methods check for existence for you.

参数

task_id (int) – an existing task ID.

返回

bool – True the task exists False if it does not.

active(task_id)[源代码]

Check if a task is active (has not been called yet).

参数

task_id (int) – an existing task ID.

返回

bool

True if a task is active (has not been called yet). False if

it is not (has been called) or if the task does not exist.

cancel(task_id)[源代码]

Stop a task from automatically executing. This will not remove the task.

参数

task_id (int) – an existing task ID.

返回

bool

True if the cancel completed successfully.

False if the cancel did not complete successfully.

remove(task_id)[源代码]

Remove a task without executing it. Deletes the instance of the task’s deferred.

参数

task_id (int) – an existing task ID.

返回

bool – True if the removal completed successfully.

clear(save=True, cancel=True)[源代码]

Clear all tasks. By default tasks are canceled and removed from the database as well.

参数
  • save=True (bool) – Should changes to persistent tasks be saved to database.

  • cancel=True (bool) – Cancel scheduled tasks before removing it from task handler.

返回

True (bool) – if the removal completed successfully.

call_task(task_id)[源代码]

Call the callback of a task. Leave the task unaffected otherwise. This does not use the task’s deferred instance. The only requirement is that the task exist in task handler.

参数

task_id (int) – an existing task ID.

返回

bool or any – Set to False if the task does not exist in task handler. Otherwise it will be the return of the task’s callback.

do_task(task_id)[源代码]

Execute the task (call its callback). If calling before timedelay cancel the deferred instance affliated to this task. Remove the task from the dictionary of current tasks on a successful callback.

参数

task_id (int) – a valid task ID.

返回

bool or any – Set to False if the task does not exist in task handler. Otherwise it will be the return of the task’s callback.

get_deferred(task_id)[源代码]

Return the instance of the deferred the task id is using.

参数

task_id (int) – a valid task ID.

返回

bool or deferred

An instance of a deferred or False if there is no task with the id.

None is returned if there is no deferred affiliated with this id.

create_delays()[源代码]

Create the delayed tasks for the persistent tasks. This method should be automatically called when Evennia starts.