Python Tutorial - 27. Multiprocessing Introduction

codebasics · Beginner ·🛠️ AI Tools & Apps ·10y ago

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
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 53 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

This tutorial introduces the basics of multiprocessing in Python, covering process creation, function execution, and the differences between multiprocessing and multithreading. Viewers will learn how to create processes, execute functions, and understand the implications of global variables in multiprocessing.

Key Takeaways
  1. Import the multiprocessing module
  2. Create a process using the Process class
  3. Define a function to be executed in the process
  4. Start the process using the start method
  5. Join the process using the join method
  6. Understand the differences between multiprocessing and multithreading
💡 Each process has its own address space, and changes made to global variables in one process do not affect other processes.

Related Reads

📰
Otter vs MeetingMinutes: Which AI Meeting Recorder Should You Choose?
Choose the best AI meeting recorder for your needs by comparing Otter and MeetingMinutes beyond transcription capabilities
Dev.to AI
📰
How are you handling AI exhaustion?
Learn how to manage AI exhaustion by automating tasks and prioritizing accountability, and discover strategies for handling the pressure of increased workload with less human oversight.
Dev.to AI
📰
How Can a Beginner Use AI to Start Making Money Online?
Learn how to leverage AI for online income as a beginner, exploring tools and business models for monetization
Medium · AI
📰
AI writing tools pricing compared (2026-08) - 8 tools, tracked daily
Compare pricing plans of 8 AI writing tools to make informed decisions for your content creation needs
Dev.to AI

Chapters (3)

Overview
0:26 Example and explanation of multiprocessing
6:40 Difference between multiprocessing and multithreading
Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →