How to influence the build order¶
Important
This guide shows how to influence the order in which tasks are executed. The feature should be treated with caution since it might make projects work whose dependencies and products are not fully specified.
You can influence the order in which tasks are executed by assigning preferences to
tasks. Use
@pytask.mark.try_first to
execute a task as early as possible and
@pytask.mark.try_last to defer
execution.
Note
A little bit more background: Tasks, dependencies and products form a directed acyclic graph (DAG). A topological ordering determines the order in which tasks are executed such that tasks are not run until their predecessors have been executed. You should not assume a fixed ordering in which tasks are executed.
As an example, here are two tasks where the decorator ensures that the output of the second task is always shown before the output of the first task.
import pytask
def task_first():
print("I'm second.")
@pytask.mark.try_first
def task_second():
print("I'm first.")
The execution yields (use -s to
make the output visible in the terminal)
$ pytask -s
────────────────────────── Start pytask session ─────────────────────────
Platform: win32 -- Python 3.13.0, pytask 0.6.0, pluggy 1.3.0
Root: C:\Users\pytask-dev\git\my_project
Collected 2 task.
I'm first
I'm second
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓
┃ Task ┃ Outcome ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩
│ <span class="termynal-dim">task_example.py::</span>task_first │ <span class="termynal-success">.</span> │
│ <span class="termynal-dim">task_example.py::</span>task_second │ <span class="termynal-success">.</span> │
└───────────────────────────────┴─────────┘
<span class="termynal-dim">─────────────────────────────────────────────────────────────────────────</span>
<span class="termynal-success">╭───────────</span> <span style="font-weight: bold;">Summary</span> <span class="termynal-success">────────────╮</span>
<span class="termynal-success">│</span> <span style="font-weight: bold;"> 2 Collected tasks </span> <span class="termynal-success">│</span>
<span class="termynal-success">│</span> <span class="termynal-success-textonly"> 2 Succeeded (100.0%) </span> <span class="termynal-success">│</span>
<span class="termynal-success">╰────────────────────────────────╯</span>
<span class="termynal-success">─────────────────────── Succeeded in 0.11 seconds ──────────────────────</span>
Replacing
pytask.mark.try_first with
pytask.mark.try_last yields
import pytask
def task_first():
print("I'm second.")
@pytask.mark.try_last
def task_second():
print("I'm first.")
and
$ pytask -s
────────────────────────── Start pytask session ────────────────────────
Platform: win32 -- Python 3.13.0, pytask 0.6.0, pluggy 1.3.0
Root: C:\Users\pytask-dev\git\my_project
Collected 2 task.
I'm second
I'm first
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓
┃ Task ┃ Outcome ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩
│ <span class="termynal-dim">task_example.py::</span>task_first │ <span class="termynal-success">.</span> │
│ <span class="termynal-dim">task_example.py::</span>task_second │ <span class="termynal-success">.</span> │
└───────────────────────────────┴─────────┘
<span class="termynal-dim">─────────────────────────────────────────────────────────────────────────</span>
<span class="termynal-success">╭───────────</span> <span style="font-weight: bold;">Summary</span> <span class="termynal-success">────────────╮</span>
<span class="termynal-success">│</span> <span style="font-weight: bold;"> 2 Collected tasks </span> <span class="termynal-success">│</span>
<span class="termynal-success">│</span> <span class="termynal-success-textonly"> 2 Succeeded (100.0%) </span> <span class="termynal-success">│</span>
<span class="termynal-success">╰────────────────────────────────╯</span>
<span class="termynal-success">─────────────────────── Succeeded in 0.11 seconds ───────────────────────</span>