Switch a Task On Mid-run
Sometimes the work itself changes during a run: from 1 minute, the shop gift wraps some orders after packing. Declare the new task from the start, route to it with a weight read from the registry, and change that weight with a scenario step. The weight starts at 0, so no order reaches the task until the step.
A blueprint and the builder create their tasks before the clock starts and have no setting that adds one later. A task declared from the start publishes nothing while no order is routed to it, so this page needs no process and no Python in the blueprint. To add a resource during a run instead, see Add Resources Mid-run.
Before You Start
- Task Flow explains
next.weightsand the expressions a weight can be. - Live Parameters explains the registry paths, such as
Shop.variables.wrap_weight. - Script an Experiment explains scenario steps.
Steps
Declare the Switch and the New Task
Declare a variable that holds the new task's weight, here wrap_weight at 0. Declare the new task's resource and service too, as for any other task: the wrappers have a unit from the start, and it stays idle until the switch.
Route to the Task by the Switch
Give the task before it a weight for the new task that reads the variable: registry["Shop.variables.wrap_weight"]. The expression is read each time an order is packed, so a change lands on the next order. Before the switch the weights are 3 to 0 and every order ships. After it they are 3 to 1, so one order in four is wrapped on average. In the low-level API, a Router with the same rules chooses the next task, and choose(..., env=env) gives it the registry.
Turn It On With a Scenario Step
A scenario step sets the variable at 1 minute. Its record publishes what it did at that time, as a ground-truth record to a sink with when: labels. To retire the old route as well, give ship_order a weight that reads a second variable, and set that variable to 0 with another step.
Check the Result
Run desflow run switch_a_task_on.yaml, python switch_a_task_on_builder.py or python switch_a_task_on_low_level.py. Each runs 5 simulated minutes at once, with an order every 5 seconds:
- Before 1 minute, every order goes from
pack_ordertoship_order, and nogift_wrappedevent appears. - From 1 minute, 48 orders are packed and 8 of them are wrapped, the first
orders-12at 65 seconds. The weights give one in four on average, and a run this short shows fewer. - The wrapped orders are the same in every form, because each draws the route from the routing task's own random stream,
route.pack_order, with the same seed.
Full Files
Full files
| docs/snippets/guides/switch_a_task_on_low_level.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |