Python Tutorial - 27. Multiprocessing Introduction
Key Takeaways
This video tutorial covers the basics of multiprocessing in Python, including creating processes, executing functions, and understanding the differences between multiprocessing and multithreading. The tutorial uses the multiprocessing module to create two processes that calculate the square and cube of numbers in an array.
Full Transcript
hello and welcome to core basics coding tutorial in this tutorial we are going to talk about multi processing in Python now if you have not already watched my video on multi processing versus multi threading then I would highly recommend watching that also watch my tutorial on multi threading once you watch these two then you can continue with the tutorial multi processing all right so here in this core what I'm going to do is I have an array of numbers and I'm going to create two different processes which will work on these numbers so the first process will calculate the square of these numbers and the second one will calculate the cube of these numbers all right fairly simple okay so let me create my first process so I will say p1 equal to multi-processing dot process okay and here in the target you need to mention the function which is going to be executed as part of this process okay so my first function will be calculate Square and I'm going to write them permutation of this function in a minute and then the second argument is the arts which is your array okay and since it is a topple you need to put a comma here okay now let's implement calculate square function okay so the input to this function is a numbers list and you are iterating through these numbers and all you are doing is just printing the square of these numbers okay so just printing and star I'm and I will create another function which will calculate the cube okay so it will be called calculate cube and it will just calculate the cube okay so my second function I am going to create a second process call it a p2 and in the target I will mention calculate Q okay now you need to start this processes so when you create a process using this instruction it doesn't get started automatically you need to call the start function on these two processes all right so after this only both of these processes will start execution one more thing you need to do is join so what join will do is it will wait until the execution of these processes is or and after that you are printing a done statement okay so that you know that you are done with both of these processes okay now if you notice this syntax it's quite similar to multi-threading so in multi-threading you import a multi-threading module and you create a thread and even threads have start and join call so if you are already familiar with threading multi-threading Python then learning multi processing will be very easy in terms of syntax code syntax at least okay so let's execute this program excellent so it printed Square and cube of these numbers all right now all of these activity happen under two different processes okay now if you want to see these processes into you windows task manager then you need to introduce some delay here so that we can see those processes while they are still executing so I'm going to just add some delay to make these functions execute little slower and then we'll open windows task manager okay so I just started the execution and you will see this thing being printed here so while it is printing open the windows task manager go to details tab and search for Python so here you see these three processes one two three one is for the parent program which is the main program and then there are two processes for p1 and p2 now they went away because the execution is complete when you are doing multi-threading you don't see the processes like this okay so that's a difference okay so far so good now instead of printing the result let's store the result in a global variable okay so Square result and to keep things simple I'm going to create only one process okay so let me remove the second process here I'm removed second process so I have now only one process and I am now going to store the result into this global variable and I'm going to okay okay and I need to define this as a global so that it luck acknowledges that it's a global variable and once this is done I'm going to put in the result here okay so let's execute this and see what happens okay I need to remove delay all right executing it all right interesting so result it says it has no elements into the array now we already see it calculated the square number so why is this blank just think for a moment why is it blank shouldn't it be all these numbers 4 9 64 81 no it won't be like that because then you create a new process what happens is it will create a new copy of square result global variable every process has their own address space so this process p1 here will copy the global state and create its own address space so that the cop the copy of square result is different and the result doesn't flow back to this global variable basically so if you print the same thing within a process then it will work so here I am going to say reading a process result so you can see that within a process it has the result or side process it doesn't have a result alright so this is the key difference between multi processing and multi threading if you were using multi threading you will see the square result being filled with all the square numbers here okay so that's the main difference okay so in this tutorial we just covered very very basic concepts about multi processing we have lot more to cover like logs mutex inter process communication methods such as queue and pipe all of those will be covered in the future tutorial all right until then please don't forget to subscribe share and comment on this tutorial alright thank you for watching
Original Description
In today’s tutorial we will learn what is multiprocessing in python. We will focus on what is multiprocessing with the help of examples and the difference between multiprocessing and multithreading.
Topics that are covered in this Python Video:
0:00 Overview
0:26 Example and explanation of multiprocessing
6:40 Difference between multiprocessing and multithreading
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Code used in this tutorial: https://github.com/codebasics/py/blob/master/Multiprocessing/multiprocessing_introduction.py
Live code: https://repl.it/ImM1
Next Video:
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value: https://www.youtube.com/watch?v=uWbSc84he2Q&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=31
Website: https://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Playlist
Uploads from codebasics · codebasics · 53 of 60
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
▶
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
More on: Tool Use & Function Calling
View skill →Related Reads
Chapters (3)
Overview
0:26
Example and explanation of multiprocessing
6:40
Difference between multiprocessing and multithreading
🎓
Tutor Explanation
DeepCamp AI