Branchprocess

The BranchProcess class is a special version of the Process class, capable of storing multiple versions of the same process. This comes in handy when the same set of tasks is to be run several times with different input. The branchprocess takes a list of tasks and the number of branches as input. Furthermore you can give it a meaningful name.

The tasks in the branchprocess are defined in a slightly different manner than for the process. To separate the arguments and keyword arguments for the different branches, they should be input as a list of lists and a list of dicts, respectively.

Under the hood of the BranchProcess

The BranchProcess automatically creates two extra objects that are added to your process: a Brancher and a Merger. The Brancher is put first, serves as a common starting point for all the tasks, and holds information about the number of branches. The Merger is put last and serves as a common ending point for the tasks.

Below is a slightly rewritten version of the previous example. Notice how the branchprocess wraps the tasks and that the branchprocess is further put in the process as a regular task.

main_branches.py
 1import caliber
 2
 3import pasta_functions
 4
 5# Define tasks
 6make_dough = caliber.Task(
 7    function=pasta_functions.make_dough,
 8    name='Make dough',
 9    args=[
10        [3],
11        [6],
12    ],
13)
14
15rest = caliber.Task(
16    function=pasta_functions.rest,
17    name='Rest',
18)
19
20roll_and_shape = caliber.Task(
21    function=pasta_functions.roll_and_shape,
22    name='Roll and shape',
23    kwargs=[
24        {'pasta_type': 'tagliatelle'},
25        {'pasta_type': 'ravioli'},
26    ]
27)
28
29# Collect tasks in a branchprocess and put in process
30pastas = caliber.BranchProcess(
31    name='Variants of pasta',
32    branches=2,
33    tasks=[
34        make_dough,
35        rest,
36        roll_and_shape,
37    ]
38)
39
40pasta = caliber.Process(
41    tasks=[
42        pastas,
43    ],
44    name='Pasta',
45)
46
47# Create workflow
48make_pasta = caliber.Workflow(
49    process=pasta,
50    name='Make pasta',
51)
52
53make_pasta.run()

When to use the BranchProcess?

The BranchProcess is a powerful option when the tasks take input files as arguments, e.g. when the tasks contain functions that call external programs that run the input files. In cases where tasks are repeated for different input, but where one task depends on the results from one of the previous, a better option could be to use the global data object and to handle the different input directly in the functions.