-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy paththursday_04.html
817 lines (474 loc) · 18.3 KB
/
thursday_04.html
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
<!DOCTYPE html>
<html>
<head>
<title>Digital Summer School 2024: THU04</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="../style.css"> </head>
<body>
<textarea id="source">
class: center, middle, titlepage
### THU04: *Multiprocessing, Logging, Containerisation, Orchestration.*
---
class: contentpage
### **Agenda**
This afternoon we are going to look at some more advanced Python topics, such as Multiprocessing and Logging.
Then we are going to look at containerisation and orchestration, which are a jump up in complexity level from what we have covered, but very useful if you want to manage many automated processes.
---
class: contentpage
### **1. Multiprocessing with Python**
Multiprocessing, in this context, refers to the fact that Python runs on a single processor.
What does this mean? A modern computer will generally have multiple "processors" to balance the load of different operations, but systems like Python are only able, by default, to draw upon one processor at a time.
To illustrate this, we can open up our activity monitor and observe activity.
---
class: contentpage
### **1. Multiprocessing with Python**
Depending on your situation, this is a missed opportunity, as you could have an extremely powerful computer where your Python process is only using a small percentage of total possible computational power.
Worth noting that most laptops have limited resources compared to higher end desktop computers, so a possible development pathway is to refine a process on your laptop and then transfer to a serious multicore desktop machine (if one is available to you).
---
class: contentpage
### **1. Multiprocessing with Python**
The goal here is that if a process takes a certain amount of time, if you can efficiently multiprocess on a 64-core machine, the process will run 64 times faster.
There is a big difference between 1 day and 64 days, or 1 year and 64 years!!!
---
class: contentpage
### **1. Multiprocessing with Python**
Python has a handy multiprocessing library called "multiprocessing".
The first thing we would want to do is so how many processors our computer has to give some idea of how we allocate resources.
```python
import multiprocessing
multiprocessing.cpu_count()
```
Also useful is that the library will automatically make use of as many "CPUs" (or processors) as are available.
---
class: contentpage
### **1. Multiprocessing with Python**
We can now define a task in Python (or a "function"), and then organise for it to run in batches.
The Pool method makes allocating tasks in batches quite easy, but note that all results will be returns when the longest has finished.
```python
import multiprocessing
import time
def my_process(file_name):
print(f"{file_name} has started.")
time.sleep(10)
print(f"{file_name} has ended.")
if __name__ == "__main__":
files = ['file_01', 'file_02', 'file_03', 'file_04']
p = multiprocessing.Pool()
with p:
p.map(my_process, files)
print("All processes have finished.")
```
---
class: contentpage
### **1. Multiprocessing with Python**
There are also a few new code components we can see here!
---
class: contentpage
### **1. Multiprocessing with Python**
```python
import multiprocessing
import time
def my_process(file_name):
print(f"{file_name} has started.")
time.sleep(10)
print(f"{file_name} has ended.")
if __name__ == "__main__":
files = ['file_01', 'file_02', 'file_03', 'file_04']
p = multiprocessing.Pool()
with p:
p.map(my_process, files)
print("All processes have finished.")
```
The `def name()` construction in Python is what we can define a function, which is a common way to group a block of code we want to reuse to do the same thing over and over, but maybe with different inputs.
This is perfect for multiprocessing, where we want to define a single block of code which gets executed in different processes at the same time.
---
class: contentpage
### **1. Multiprocessing with Python**
```python
import multiprocessing
import time
def my_process(file_name):
print(f"{file_name} has started.")
time.sleep(10)
print(f"{file_name} has ended.")
if __name__ == "__main__":
files = ['file_01', 'file_02', 'file_03', 'file_04']
p = multiprocessing.Pool()
with p:
p.map(my_process, files)
print("All processes have finished.")
```
The `if __name__ == "__main__":` part is a common way of making sure that the Python script is being executed directly, and not being imported into other code.
If you recall, a "library" is just someone else's code we have imported, and this is an explicit mechanism to avoid this happening accidentally.
---
class: contentpage
### **1. Multiprocessing with Python**
```python
import multiprocessing
import time
def my_process(file_name):
print(f"{file_name} has started.")
time.sleep(10)
print(f"{file_name} has ended.")
if __name__ == "__main__":
files = ['file_01', 'file_02', 'file_03', 'file_04']
p = multiprocessing.Pool()
with p:
p.map(my_process, files)
print("All processes have finished.")
```
If we run this, we will observe that four processes start simultaneously, perform the action which is to "sleep" (do nothing) for ten seconds, and the processes are all returned at the same time.
---
class: contentpage
### **1. Multiprocessing with Python**
Practical: Let us now write a script to hash all the image files in our film scan, then write a multiprocessing variant and observe the speed difference.
Hint: An easy way to get timestamps is using the datetime library
```python
import datetime
print(datetime.datetime.now())
```
---
class: contentpage
### **1. Multiprocessing with Python**
<b>
<tspan style="color:black">PAGE INTENTIONALLY BLANK</tspan>
</b>
---
class: contentpage
### **1. Multiprocessing with Python**
Solutions: Hashing all files in film scan sample.
```python
import datetime
import hashlib
import pathlib
start_time = datetime.datetime.now()
files = pathlib.Path.cwd() / 'media' / 'film_scan'
for file in files.iterdir():
with open(file, 'rb') as f:
file_content = f.read()
result = hashlib.md5(file_content).hexdigest()
end_time = datetime.datetime.now()
print('processing time:', end_time-start_time)
```
---
class: contentpage
### **1. Multiprocessing with Python**
Solutions: Multiprocess hashing all files in film scan sample.
```python
import datetime
import multiprocessing
import time
def my_process(file_name):
with open(file_name, 'rb') as f:
file_content = f.read()
result = hashlib.md5(file_content).hexdigest()
if __name__ == "__main__":
start_time = datetime.datetime.now()
files = pathlib.Path.cwd() / 'media' / 'film_scan'
files = [x for x in files.iterdir()]
p = multiprocessing.Pool()
with p:
p.map(my_process, files)
end_time = datetime.datetime.now()
print('processing time:', end_time-start_time)
```
---
class: contentpage
### **1. Multiprocessing with Python**
Multiprocessing is extremely useful if you have "expensive" operations (like hashing!) which are causing you to wait for your script to finish.
It does however introduce extra complexity to your scripts, so should only be used if required.
---
class: contentpage
### **2. Logging with Python**
Logging is an important feature when we build a complex pipeline and something goes wrong, or if we want to check up on where a process is up to!
Python has an inbuilt `logging` library!
---
class: contentpage
### **2. Logging with Python**
Logging is quite similar to "print" statements, but intended to report activity that can be consulted later, whereas print statements are ephemeral.
It is also extremely useful if you ever get asked the questions: when did you exactly transcode that file? How long did it take? What was the output?
---
class: contentpage
### **2. Logging with Python**
We can do all kinds of experiments with logging, but for now we are just going to apply it to our multiprocessing hashing example.
Note that logging has a built-in five levels of severity: DEBUG, INFO, WARNING, ERROR, CRITICAL.
An example of debug logs would be "this variable equalled this value", while an error would be something like the `raise Exceptions` we saw earlier.
---
class: contentpage
### **2. Logging with Python**
The simplest example of logging is something like:
```python
import logging
logging.info('hello')
```
Note that this will not print anything.
---
class: contentpage
### **2. Logging with Python**
We can configure to write to a file, and formatting the logs like this
```python
import logging
logger = logging.getLogger()
file_handler = logging.FileHandler('app.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.error("There is an error!")
```
---
class: contentpage
### **2. Logging with Python**
Now for our practical, can we add this to our previous hashing example!
---
class: contentpage
### **2. Logging with Python**
<b>
<tspan style="color:black">PAGE INTENTIONALLY BLANK</tspan>
</b>
---
class: contentpage
### **2. Logging with Python**
```python
import hashlib
import logging
import multiprocessing
import pathlib
import time
def my_process(file_name):
logging.info(f'{file_name} started.')
with open(file_name, 'rb') as f:
file_content = f.read()
result = hashlib.md5(file_content).hexdigest()
logging.info(f'{file_name} finished.')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('app.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if __name__ == "__main__":
files = pathlib.Path.cwd() / 'media' / 'film_scan'
files = [x for x in files.iterdir()]
p = multiprocessing.Pool()
with p:
p.map(my_process, files)
```
---
class: contentpage
### **3. Containerisation**
Containerisation refers to the process of encapsulating an environment to ensure that systems run predictably.
Why would you do this? Quite simply because every computer in the world, once it has been used for a significant period of time, becomes its own environment.
Files sit in different locations, applications are installed or removed, etc.
There are also inherent differences we have seen in different software versions, different operating systems, differences, differences, differences.
Containerisation is about building a fully predictable computing environment.
On our own computers.
Amazing!
---
class: contentpage
### **3. Containerisation**
Containerisation is also useful if you want a build a system which requires installing a lot of smaller applications, and configuration (as we have done in this course) and you want it to deploy predictably.
---
class: contentpage
### **3. Containerisation**
Docker is ubiquitous in this space.
Podman has some benefits around security considerations, so may be more attractive to your IT team.
Test Docker is installed.
```sh
docker --version
```
---
class: contentpage
### **3. Containerisation**
Then we need to create a cool script. This doesn't have to be Python, but we have been doing so much work with Python so far seems good to stick with.
Here is a simple script which calls the NFSA collection API and prints out a JSON file.
```python
# script.py
import requests
result = requests.get('https://api.collection.nfsa.gov.au/title/331321')
print(result.status_code)
print(result.text)
```
---
class: contentpage
### **3. Containerisation**
A core component of working with Docker is the idea of the Dockerfile.
We can think of this exactly the same as a cooking recipe.
Here is an example - don't worry if it looks unfamiliar, this is special docker syntax!
Dockerfile
```docker
FROM python:3.11
ADD script.py .
RUN pip install requests
CMD python ./script.py
```
---
class: contentpage
### **3. Containerisation**
We can now `build` the container. This means that all the setup required from the Dockerfile is run, but the process itself is not.
What we are doing here is essentially "cooking" a custom computer which is based on a "python" image.
```sh
docker build -t script_container -f Dockerfile .
```
This will take a bit!
---
class: contentpage
### **3. Containerisation**
Once completed you can run
```sh
docker images
```
And you will see all the "docker images". Note these are not running, they are frozen state computational environments.
---
class: contentpage
### **3. Containerisation**
If we want to actually run the image, i.e. boot up the computer we have created, we run
```sh
docker run script_container
```
And we should see API output.
---
class: contentpage
### **3. Containerisation**
Okay, but so what? What have we gained from running it locally as a Python script?
Well, this is where it gets cool!
Run
```sh
docker run --name mariadb-instance -p 3306:3306 -e MARIADB_ROOT_PASSWORD=password -d mariadb
```
You are now running the popular MariaDB database on your computer without having to go through any manual steps.
```sh
docker ps
```
---
class: contentpage
### **3. Containerisation**
You can also log into it, and have a look around!
```
docker exec -it mariadb-instance /bin/bash
```
---
class: contentpage
### **3. Containerisation**
One last example, say we can also install whole operating systems through Docker, and then install tools on top
Here is a new Dockerfile which uses Ubuntu, a popular Linux distro, and installs FFmpeg
```docker
FROM ubuntu:22.04
RUN apt update
RUN apt-get install -y ffmpeg
```
---
class: contentpage
### **3. Containerisation**
We now build a new docker image using this docker file
```sh
docker build -t ffmpeg_docker -f Dockerfile .
```
Once this is complete, we can see the new image available to us.
```sh
docker images
```
We can run it
```sh
docker run -t -d ffmpeg_docker
```
---
class: contentpage
### **3. Containerisation**
See it running
```sh
docker ps
```
And log into it and run FFmpeg commands
```sh
docker exec -it ffmpeg_docker /bin/bash
```
There are loads of cool things you can do with Docker, hopefully this has been inspiring!
Docker is useful if you want to deploy complex applications (say you are trying to deploy a system which needs a database, an API and a user interface), if you want to ensure that systems behave predictably, or if you want to deploy systems with minimal manual installation steps.
---
class: contentpage
### **4. Orchestration: Airflow**
The last topic for today is orchestration. We have covered writing and scheduling individual tasks, which is fine if you are running one or two handfuls of processes.
Once you scale up to fifty or a hundred distinct processes this can be hard to manage, keeping track of what ran when, failures.
What we need then is a system to manage all of our processes, which is generally called an "orchestrator".
This is an interesting subject, as I believe Joanna and I are both at the point where our own processes have only recently reached this moment, so these tools are new for us as well.
---
class: contentpage
### **4. Orchestration: Airflow**
The most popular of these tools is Apache Airflow, a complex system which we are going to use Docker to set up quickly!
Let's `curl` the "docker compose" file. What is docker compose?
In the last example we were building and deploying single containers, docker compose lets us define multiple containers in one configuration document.
```sh
mkdir airflow
cd airflow
curl -LfO 'https://airflow.apache.org/docs/apache-airflow/2.10.1/docker-compose.yaml'
```
---
class: contentpage
### **4. Orchestration: Airflow**
Make required airflow containers
```sh
mkdir -p ./dags ./logs ./plugins ./config
```
Create a required env file.
```sh
echo -e "AIRFLOW_UID=$(id -u)" > .env
```
Start the database
```sh
docker compose up airflow-init
```
Start the system
```sh
docker compose up -d
```
---
class: contentpage
### **4. Orchestration: Airflow**
Before we have a look at the user interface, worth considering what is actually happening here.
Docker containers are running a moderately complex of services which ultimately run processes or workflows defined in Python.
---
class: contentpage
### **4. Orchestration: Airflow**
This should now be running and accessible through the browser at http://localhost:8080 (this may take a minute or two).
Login is "airflow/airflow".
---
class: contentpage
### **4. Orchestration: Airflow**
Lets look at `example_python_decorator`
Generally speaking, there is a single python script per DAG, which defines the recipe of processes we want to perform.
We can click into the DAG and see individual tasks, these are all simply functions in a single Python script.
We have options for inputs, and whether it is scheduled on a timer, or by a "sensor" or we start them manually.
---
class: contentpage
### **4. Orchestration: Airflow**
There are many orchestration options, and so I just wanted to very quickly deploy two alternatives to Airflow, Kestra and Dagster.
I haven't spent much time in either of these, but it is just to show that the user interface for all of these systems is ultimatley very similar.
---
class: contentpage
### **4. Orchestration: Kestra**
Kestra is a newer orchestration project, with paid features which work against it (unlike Airflow), but it does have a slick user interface.
```sh
mkdir kestra
cd kestra
curl -o docker-compose.yml https://raw.githubusercontent.com/kestra-io/kestra/develop/docker-compose.yml
docker compose up -d
```
Service should be available at http://localhost:8080
---
class: contentpage
### **4. Orchestration: Dagster**
Dagster is very popular in the machine learning space as an orchstrator for model training, I haven't really ever used it!
```sh
git clone https://github.com/dagster-io/dagster.git
cd dagster/examples/deploy_docker/
docker compose up -d
```
Service should be available at http://localhost:3000
---
class: center, middle, titlepage
### End of Day Four.
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">var slideshow = remark.create({ratio: "16:9"});</script>
</body>
</html>