Difference between revisions of "TaskFlow/Task Arguments and Results"
(started rewrite (almost) from scratch to make into documentation) |
|||
Line 1: | Line 1: | ||
− | == | + | == Overview == |
− | + | In TaskFlow, all flow state should go to storage. That includes | |
+ | all the information that task needs when it is executed (task arguments), and | ||
+ | all the information task produces (task results). Developer who implements task | ||
+ | or flow can specify what arguments task accepts and what result it returns | ||
+ | in several ways. | ||
− | + | Set of names of task arguments is available as <code>requires</code> property | |
+ | of the task instance. When task is about to be executed values with this names | ||
+ | are retrieved from storage and passed to <code>execute</code> method of the task | ||
+ | as keyword arguments. | ||
− | + | Set of names of task results (what task provides) is available as | |
− | <code>execute</code> method | + | <code>provides</code> property of task instance. After task finishes |
+ | successfully, it's result(s) (what task <code>execute</code> method returns) are | ||
+ | available by these names from storage (there will be examples below). | ||
− | + | == Arguments Specification == | |
− | |||
− | + | There are different way to specify task argument set. | |
− | |||
− | == | + | === Arguments Inference === |
− | Task | + | Task arguments can be inferred from arguments of <code>execute</code> method of |
− | + | the task. For example: | |
− | task | ||
− | + | >>> class MyTask(task.Task): | |
− | + | ... def execute(self, spam, eggs): | |
+ | ... return spam + eggs | ||
+ | ... | ||
+ | >>> MyTask().requires | ||
+ | set(['eggs', 'spam']) | ||
− | + | Inference from signature is simplest way to specify task arguments. | |
− | + | Optional arguments (with default values), and special arguments like | |
− | + | <code>self</code>, <code>*args</code> and <code>**kwargs</code> are | |
+ | ignored on iferrence: | ||
− | + | >>> class MyTask(task.Task): | |
+ | ... def execute(self, spam, eggs=()): | ||
+ | ... return spam + eggs | ||
+ | ... | ||
+ | >>> MyTask().requires | ||
+ | set(['spam']) | ||
+ | >>> | ||
+ | >>> class UniTask(task.Task): | ||
+ | ... def execute(self, *args, **kwargs): | ||
+ | ... pass | ||
+ | ... | ||
+ | >>> UniTask().requires | ||
+ | set([]) | ||
− | + | === Manually Specifying Requirements === | |
− | |||
− | |||
− | |||
− | + | === Rebind === | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | == | + | == Results Specification == |
− | |||
− | == | + | === Returning One Value === |
− | + | === Returning Tuple === | |
− | |||
− | |||
− | + | === Returning Dictionary === | |
− | + | === Default Provides === | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | == | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 12:54, 21 October 2013
Contents
Overview
In TaskFlow, all flow state should go to storage. That includes all the information that task needs when it is executed (task arguments), and all the information task produces (task results). Developer who implements task or flow can specify what arguments task accepts and what result it returns in several ways.
Set of names of task arguments is available as requires
property
of the task instance. When task is about to be executed values with this names
are retrieved from storage and passed to execute
method of the task
as keyword arguments.
Set of names of task results (what task provides) is available as
provides
property of task instance. After task finishes
successfully, it's result(s) (what task execute
method returns) are
available by these names from storage (there will be examples below).
Arguments Specification
There are different way to specify task argument set.
Arguments Inference
Task arguments can be inferred from arguments of execute
method of
the task. For example:
>>> class MyTask(task.Task): ... def execute(self, spam, eggs): ... return spam + eggs ... >>> MyTask().requires set(['eggs', 'spam'])
Inference from signature is simplest way to specify task arguments.
Optional arguments (with default values), and special arguments like
self
, *args
and **kwargs
are
ignored on iferrence:
>>> class MyTask(task.Task): ... def execute(self, spam, eggs=()): ... return spam + eggs ... >>> MyTask().requires set(['spam']) >>> >>> class UniTask(task.Task): ... def execute(self, *args, **kwargs): ... pass ... >>> UniTask().requires set([])