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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606# **What is a `flow` and a `task`?**
### **Basic building blocks**
In Prefect, [Python is the API](https://www.prefect.io/guide/blog/announcing-prefect-orion/). You can just write Python. But to organize the steps, control their execution state, and get more [visibility](https://medium.com/the-prefect-blog/scheduled-vs-event-driven-data-pipelines-orchestrate-anything-with-prefect-b915e6adc3ba), there are two core concepts that serve as an **entrypoint** to orchestration:
1. `Task`—an (optional) function that represents a discrete `smallest` unit of work
2. `Flow`— a container for business logic and orchestration rules for any executable/schedulable process
You may combine `tasks`, `flows`, and `subflows` into composable and modular workflows in various ways depending on what you are trying to achieve.
<aside>
👇 To To understand those various ways, let’s get hands-on
</aside>
### Exercise: Hello World
```python
from prefect import flow
@flow(log_prints=True)
def hi():
print("Hi from Prefect, LiveEO! 🤗")
if __name__ == "__main__":
hi()
```
### Exercise: Hello World with a `task`
```python
from prefect import flow, task, get_run_logger
@task
def hello_world():
logger = get_run_logger()
logger.info("Hello from the workshop!")
@flow(log_prints=True)
def hi():
print("Hi from Prefect, LiveEO! 🤗")
hello_world()
if __name__ == "__main__":
hi()
```
### Exercise: Hello World with a `mapped` task
```python
from prefect import flow, task
from typing import List
@task
def hello_world(user: str):
print(f"✨ Hello from the workshop, {user}! 👋 📚")
@flow(log_prints=True)
def hi(
users: List[str] = [
"Marvin",
"Anna",
"Toby",
"Rebecca",
"Amelie",
"Carlo",
"Christian V.",
"Christian R.",
"Fernando",
"Francesco",
"Jorge",
"Justin",
"Laxmi",
"Miguel",
"Wieger",
"Tobias",
]
):
print("Hi from Prefect, LiveEO! 🤗")
hello_world.map(users)
if __name__ == "__main__":
hi()
```
### Exercise: Hello World with a `for` loop running concurrently
```python
from prefect import flow, task
from typing import List
@task
def hello_world(user: str):
print(f"✨ Hello from the workshop, {user}! 👋 📚")
@flow(log_prints=True)
def hi(
users: List[str] = [
"Marvin",
"Anna",
"Toby",
"Rebecca",
"Amelie",
"Carlo",
"Christian V.",
"Christian R.",
"Fernando",
"Francesco",
"Jorge",
"Justin",
"Laxmi",
"Miguel",
"Wieger",
"Tobias",
]
):
print("Hi from Prefect, LiveEO! 🤗")
for user in users:
hello_world.with_options(name=user).submit(user)
if __name__ == "__main__":
hi()
```
### Q: can you call a `task` from another `task`?
since tasks are the smallest units already, [you can’t call tasks from other tasks](https://discourse.prefect.io/t/can-i-call-a-task-from-another-task/1453), but you can call `task.fn()` - you can do the same from both a task and a flow
### Q: what are the downsides of calling a `task` from another `task`?
To answer that, run the following flow:
```python
from prefect import task, flow
@task
def special_data():
return 42
@task
def extract_data():
return special_data.fn()
@flow(log_prints=True)
def get_data():
x = extract_data()
print(x)
if __name__ == "__main__":
get_data()
```
### Solution
you can call a task functions using `task.fn()` but you would lose visibility
Go to the UI and validate that `special_data` task is not visible in the UI
If you go to the UI you’ll see that this task ran with `task.fn()` is not visible in the UI
### Exercise: try the following example in `ipython`
```python
ipython
```
```python
from prefect import task, flow
@task
def extract_data():
return 42
@flow
def get_data():
return 42
```
```python
extract_data()
```
should fail with:
```
RuntimeError: Tasks cannot be run outside of a flow.
To call the underlying task function outside of a flow use `task.fn()`.
```
Following this advice, it works:
```python
extract_data.fn()
```
```python
get_data.fn()
```
### Q: can you call a `flow` from other `flow`?
you can call `subflows` from any of your (*parent*) flows
### Exercise: call a flow from another flow + validate in the UI that both flows got executed
```python
from prefect import task, flow
@flow
def extract_data():
return 42
@flow(log_prints=True)
def get_data():
x = extract_data()
print(x)
if __name__ == '__main__':
get_data()
```

### Exercise: navigate to the UI to the child flow `extract_data`: can you find out which `parent flow run` triggered that `child flow run`?

# **When to use a `flow, task vs. subflow`?**
- You always need a `flow`
- You may optionally add a task for extra `visibility`, `caching`, tag-based **`concurrency` limits**, concurrent & `parallel` execution
- You may add `subflows` to `group` related tasks within a flow + allow `reusing` the same group of tasks in multiple places
We’ll talk about caching and task runners in more detail later, so let’s look now at the **task-level concurrency** with an exercise
## Exercise: add a `tag` called `“db”` and ensure that no more than 3 task runs of that task are executed concurrently
```bash
prefect concurrency-limit --help # this will help you figure out the right command to create that task-level concurrency limit
```
```python
import time
from prefect import task, flow
@task(tags=["db"])
def db_task():
print("using DB connection, need to mitigate DB pressure")
time.sleep(10)
@flow(log_prints=True)
def db_flow():
for i in range(1, 10):
db_task.with_options(name=f"db_{i}").submit()
if __name__ == "__main__":
db_flow()
```
### Solution
```bash
prefect concurrency-limit create db 3
```
Flow with task using that concurrency limit:
```python
import time
from prefect import task, flow
@task(log_prints=True, tags=["db"])
def db_task():
print("using DB connection, need to mitigate DB pressure")
time.sleep(10)
@flow
def db_flow():
for i in range(1, 10):
db_task.with_options(name=f"db_{i}").submit()
if __name__ == "__main__":
db_flow()
```
Soon, you’ll be able to configure and monitor task run concurrency slots from the UI!
## Q: can you pass data between tasks?
yes, a simple example:
```python
from prefect import task, flow
@task
def extract() -> int:
return 42
@task
def transform(x: int) -> int:
return x * 2
@task
def load(x: int) -> None:
print(x)
@flow(log_prints=True)
def etl_oneliner():
load(transform(extract()))
```
## Q: What’s the difference between those 3 different ways of calling tasks within a flow?
Tasks:
```python
from prefect import task, flow
@task
def extract() -> int:
return 42
@task
def transform(x: int) -> int:
return x * 2
@task
def load(x: int) -> None:
print(x)
```
Flow 1: oneliner
```python
@flow(log_prints=True)
def etl_oneliner():
load(transform(extract()))
```
- Full example
```python
from prefect import task, flow
@task
def extract() -> int:
return 42
@task
def transform(x: int) -> int:
return x * 2
@task
def load(x: int) -> None:
print(x)
@flow(log_prints=True)
def etl():
load(transform(extract()))
if __name__ == "__main__":
etl()
```
Flow 2: cleaner
```python
@flow(log_prints=True)
def etl():
e = extract()
t = transform(e)
load(t)
```
- Full example
```python
from prefect import task, flow
@task
def extract() -> int:
return 42
@task
def transform(x: int) -> int:
return x * 2
@task
def load(x: int) -> None:
print(x)
@flow(log_prints=True)
def etl():
e = extract()
t = transform(e)
load(t)
if __name__ == "__main__":
etl()
```
Flow 3: cleaner & submitted to TaskRunner
```python
@flow(log_prints=True)
def etl_with_tr():
e = extract.submit()
t = transform.submit(e)
load.submit(t)
```
- Full example
```python
from prefect import task, flow
@task
def extract() -> int:
return 42
@task
def transform(x: int) -> int:
return x * 2
@task
def load(x: int) -> None:
print(x)
@flow(log_prints=True)
def etl():
e = extract.submit()
t = transform.submit(e)
load.submit(t)
if __name__ == "__main__":
etl()
```
## Solution to the difference
Answer: lineage + tracking of data dependencies with small objects like small integers
[https://github.com/PrefectHQ/prefect/issues/7777](https://github.com/PrefectHQ/prefect/issues/7777)


Problem: we can’t track small integers because Python uses the same memor address for every instance.
The same lineage works for dataframes even without returning states or submitting tasks to a task runner:
```python
from prefect import flow, task
import pandas as pd
@task
def get_dataframe() -> pd.DataFrame:
return pd.DataFrame(data={"Users": ["Marvin", "You"]})
@task
def add_points(df: pd.DataFrame) -> pd.DataFrame:
df["Karma_Points"] = [-42, 100]
return df
@flow
def process_data() -> pd.DataFrame:
df = get_dataframe()
return add_points(df)
if __name__ == "__main__":
result = process_data()
print(result)
```

A good workaround for this lineage problem is to return state when passing small int values as data dependencies:
```python
from prefect import task, flow
@task
def extract() -> int:
return 42
@task
def transform(x: int) -> int:
return x * 2
@task
def load(x: int) -> None:
print(x)
@flow(log_prints=True)
def etl():
e = extract(**return_state=True**)
t = transform(e, return_state=True)
load(t, return_state=True)
if __name__ == "__main__":
etl()
```
## Q: can you pass data between subflows and tasks?
yes, a simple example:
```python
from prefect import flow
@flow
def extract_data():
return 42
@flow
def transform_data(x: int) -> int:
return x * 2
@flow(log_prints=True, name="ETL with subflows")
def main():
x = extract_data()
y = transform_data(x)
print(x)
print(y)
if __name__ == "__main__":
main()
```
- **Challenge**: what’s the difference betwee the above flow and this one? **Tip: check the UI and logs!**
```python
from prefect import flow
@flow
def extract_data():
return 42
@flow
def transform_data(x: int) -> int:
return x * 2
@flow(log_prints=True, name="Parent ETL flow with subflows")
def main():
x = extract_data(return_state=True)
y = transform_data(x, return_state=True)
print(x.result())
print(y.result())
if __name__ == "__main__":
main()
```
Additionally using a task:
```python
from prefect import flow, task
@flow
def extract_data():
return 42
@flow
def transform_data(x: int) -> int:
return x * 2
@task
def load(x: int, y: int) -> None:
print(x)
print(y)
@flow(log_prints=True, name="ETL with subflows")
def main():
x = extract_data()
y = transform_data(x)
load(x, y)
if __name__ == "__main__":
main()
```
## "When should I use a subflow instead of a task?"
### Task
We recommend writing `tasks` that do a discrete unit of work in your workflow:
- calling an `API`
- performing a `database` operation
`Prefect tasks` are well suited to `parallel` or distributed execution using Dask or Ray. For `troubleshooting`, the more granular you create your tasks, the easier it is to find and fix issues.
### Subflow
`Subflows` enable you to `group` related tasks within your workflow.
Here are some `scenarios` where you might choose to use a subflow rather than calling tasks individually:
- Observability: you'll see subflow status in the **Flow Runs** dashboard rather than as tasks within a specific flow run.
- Conditional flows: If you have a **group** of tasks that run only under certain **conditions**, you can group them within a subflow and conditionally run the subflow rather than each task individually.
- Parameters: Flows have first-class support for `parameterization` + **pydantic `validation` of input parameters**, making it easy to run the same group of tasks in different use cases by simply passing different parameters to the subflow in which they run.
- Task runners: Subflows enable you to specify the task runner used for tasks within the flow. For example, if you want to optimize **parallel execution** of certain tasks with Dask/Ray, you can group them in a subflow that uses the Dask/Ray task runner. `You can use a different task runner for each subflow`.