python.task_manager.background_task module

class BackgroundTask(task_id, cbl, group, priority, args, kwargs)[source]

Bases: object

Container class for a single task.

A task is a Python callable (function/method/class) that takes some arguments, does some work and returns its result. Each task will be run in a thread and tasks will be executed in priority order.

For example:

def task_fetch_status():
    return status_of_something()
...
task_manager.add_task(task_fetch_status)
...
def on_task_completion(task, group, result):
    status = result
    # do something with the status

Additionally, tasks can be chained together so that the output of one task can be passed directly to the input of one or more downstream tasks. To achieve this, the upstream task must returns it’s result as a dictionary and this dictionary is added to the named parameters of any downstream tasks by the task manager. Care should be taken when constructing these tasks that the result of one upstream task doesn’t unintentionally overwrite any existing named parameters for a downstream task.

For example:

def task_fetch_status():
    return {"status":status_of_something()}
def task_do_something(status):
    result = None
    if status:
        result = result_of_doing_something()
    return result
...
status_task_id = task_manager.add_task(task_fetch_status, priority=1)
work_task_id = task_manager.add_task(task_do_something, priority=2, upstream_task_ids = [status_task_id])
...
def on_task_completion(task, group, result):
    if task.id = work_task_id:
        # do something with the result
        ...

Upstream tasks can be fed into multiple down-stream tasks and the task priority can also be different so for example all status fetches could be set to happen before all do-somethings by setting the priority accordingly. Down-stream tasks will also not start before it’s upstream tasks have completed.

append_upstream_result(result)[source]

Append the result from an upstream task to this tasks kwargs. In order for the result to be appended to the task it must be a dictionary. Each entry in the result dictionary is then added to the tasks named parameters so care should be taken when building the tasks that named parameters for a downstream task are not unintentionally overwritten by the result of an upstream task.

Parameters:result – A dictionary containing the result from an upstream task. If result is not a dictionary then it will be ignored.
group
Returns:The group this task belongs to
priority
Returns:The priority for this task
run()[source]

Perform this task

Returns:The result of performing the task
uid
Returns:The unique id of this task