taps.engine.task¶
ExceptionInfo
dataclass
¶
ExecutionInfo
dataclass
¶
ExecutionInfo(
hostname: str,
execution_start_time: float,
execution_end_time: float,
task_start_time: float,
task_end_time: float,
input_transform_start_time: float,
input_transform_end_time: float,
result_transform_start_time: float,
result_transform_end_time: float,
)
Task execution information.
All times are Unix timestamps recorded on the worker process executing the task. The times are as follows:
+ execution_start_time
| + input_transform_start_time
| | # Resolve task arguments
| + input_transform_end_time
|
| + task_start_time
| | # Execute task function
| + task_end_time
|
| + result_transform_start_time
| | # Transform task function result
| + result_transform_end_time
+ execution_end_time
Parameters:
-
hostname
(str
) –Name of the host the task was executed on.
-
execution_start_time
(str
) –Unix timestamp indicating the task began execution on a worker.
-
execution_end_time
(str
) –Unix timestamp indicating the task finished execution on a worker.
-
task_start_time
(str
) –Unix timestamp indicating the start of execution of the task function.
-
task_end_time
(str
) –Unix timestamp indicating the end of execution of the task function.
-
input_transform_start_time
(str
) –Unix timestamp indicating the start of resolving input arguments on the worker.
-
input_transform_end_time
(str
) –Unix timestamp indicating the end of resolving input arguments on the worker.
-
result_transform_start_time
(str
) –Unix timestamp indicating the start of transforming the task function result on the worker.
-
result_transform_end_time
(str
) –Unix timestamp indicating the end of transforming the task function result on the worker.
TaskInfo
dataclass
¶
TaskInfo(
task_id: str,
name: str,
parent_task_ids: List[str],
submit_time: float,
received_time: Optional[float] = None,
success: Optional[bool] = None,
exception: Optional[ExceptionInfo] = None,
execution: Optional[ExecutionInfo] = None,
)
Task execution information.
Parameters:
-
task_id
(str
) –Unique UUID of the task as determined by the engine.
-
name
(str
) –Name of the task. Typically defaults to the name of the function unless overridden.
-
parent_task_ids
(str
) –UUIDs of parent tasks. A task is a child task if its arguments contain a future to the result of another task.
-
submit_time
(str
) –Unix timestamp indicating the engine submitted the task to the executor.
-
received_time
(str
, default:None
) –Unix timestamp indicating the executor was notified the task has completed. This is recorded in a callback on the task future and thus includes any lag in invoking the future callbacks.
-
success
(str
, default:None
) –Boolean indicating if the task completed without raising an exception.
-
exception
(str
, default:None
) –Task exception information.
-
execution
(str
, default:None
) –Task execution information.
TaskResult
¶
TaskResult(value: R, info: ExecutionInfo)
Bases: Generic[R]
Task result structure.
Parameters:
-
value
(R
) –The result of the task's function.
-
info
(ExecutionInfo
) –Task execution information.
Source code in taps/engine/task.py
Task
¶
Bases: Generic[P, R]
, Protocol
Task protocol for a wrapped function.
Note
This is just a Protocol
to define the behavior
of a task, which is ultimately just a wrapper around a function.
A task can be created with the @task()
decorator. A task has different behavior based on if it is being called
directly or as a task executed by the Engine
.
- When called directly, the value of
_transformer
defaults toNone
, so the wrapped function is just invoked directly and the result returned. Here, the return type isR
. - When executed by the
Engine
,_transformer
is notNone
, so the wrapped function will be executed with the additional task management. Here, the return type isTaskResult[R]
.
Attributes:
-
name
(str
) –Name of the task used for logging.
__call__
¶
__call__(
*args: args,
_transformer: TaskTransformer[Any] | None = None,
**kwargs: kwargs
) -> TaskResult[R] | R
Execute the task or wrapped function.
Parameters:
-
args
(args
, default:()
) –Positional arguments to pass to the wrapped function.
-
_transformer
(TaskTransformer[Any] | None
, default:None
) –Transformer to use when resolving task arguments and transforming task results. This should never be provided by user code; this is only used when submitted as a task by the
Engine
. -
kwargs
(kwargs
, default:{}
) –Keyword arguments to pass to the wrapped function.
Returns:
-
TaskResult[R] | R
–The result of type
R
from the wrapped function, possible wrapped in aTaskResult[R]
if invoked by theEngine
.
Source code in taps/engine/task.py
task
¶
task(
function: Callable[P, R] | None = None,
*,
name: str | None = None,
wrap: bool | None = None
) -> (
Callable[[Callable[P, R]], Task[P, R]]
| Task[P, R]
)
Turn a function in a task.
A task represents a wrapped, callable object (e.g., a function) that is
executed on a worker by the Engine
. This wrapper
will perform additional task management, such as recording metrics and
transforming task parameters and return values.
Note
For convenience, this decorator/function is re-exported in
taps.engine
.
Note
The wrapped function can still be invoked directly, such as within unit
tests or when not submitted to the Engine
.
In this case, none of the additional task management is performed.
Tip
Decorating top-level functions that will be submitted to the
Engine
by an application with the @task
decorator is generally recommended.
Example
Use as a decorator (parenthesis are required).
Create a new task from a function:Failure
The following pickling errors may occur in certain scenarios.
PicklingError: Can't pickle <function ...>: it's not the same object as <...>
AttributeError: Can't pickle local object 'task.<locals>.wrapped'
If using task()
as a decorator, ensure you are calling the
decorator (e.g., with parenthesis).
Otherwise, try changing the value of the wrap
argument.
Parameters:
-
function
(Callable[P, R] | None
, default:None
) –Optional function to wrap. If not provided, this function acts like a decorator factory, returning a new callable.
-
name
(str | None
, default:None
) –Optional name for the task. Defaults to the
__name__
of the wrapped function. -
wrap
(bool | None
, default:None
) –Mutate the wrapper function to looked like the wrapped function using
functools.wraps()
. IfFalse
, afunctools.partial
function is returned instead. The default valueNone
attempts to infer the best choice based on used:wrap=True
when used as a decorator (e.g.,@task()
) andwrap=False
when used to directly create a task (e.g.,foo_task = task(foo)
).
Source code in taps/engine/task.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
|