main
ies_pi_worker.main
Task
A task is a function that's executed in background.
It's defined by an id and a Future.
When the task is initialized, started_at is populated with the current
timestamp, and as soon as the future is done, finished_at is populated in
the same way.
Source code in src/ies_pi_worker/main.py
elapsed
finished
to_dict
to_dict returns a dict representation of the task. Along with id,
started_at and finished_at it also returns the result or the error
of the task, if it has finished.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
The number of seconds to wait for the result if the task isn't finished yet. Defaults to 0.01 |
0.01
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
{ "id": str, "started_at": datetime, "finished_at": datetime, "result": any | None, "error": str | None, |
dict
|
} |
Source code in src/ies_pi_worker/main.py
Worker
Worker is a wrapper around concurrent.futures that allows to run tasks
and retrieve their results later.
It saves the tasks in memory, so they don't persist.
The tasks are short-lived, and after they're finished, they are available for a limited amount of time.
Source code in src/ies_pi_worker/main.py
get
get retrieves a Task by their id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
The id of the task. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Task |
Task
|
The Task obtained |
Source code in src/ies_pi_worker/main.py
run
run allows to run any arbitrary task, much like the submit method of
a concurrent.futures executor, but returns a Task, that will automatically
update with the result of the task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
function
|
a callable to be executed as fn(args, *kwargs). its return values will be stored in the returned Task. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Task |
Task
|
a Task that will automatically update with the result of the function. |