Multiprocessing in Python - Advanced Python 17 - Programming Tutorial

Patrick Loeber · Intermediate ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

The video demonstrates the use of the multiprocessing module in Python for parallel processing, including creating and running multiple processes, sharing data between processes, using locks to prevent race conditions, and utilizing queues for process-safe data exchange. The tutorial covers advanced topics such as process pools, map, apply, join, and close methods, and provides examples of executing functions in parallel using pool.map.

Full Transcript

hi everybody welcome to a new python tutorial in the last two tutorials we learned the difference between threats and processes and how we can use the pert and threading module in this video we will go into more detail about the multi-processing module so we will quickly recap how we can create and start multiple processes then we will learn how we can share data between processes and we will recap how to use locks to prevent race conditions and how to use queues and at the end we will learn how to use a process pool to easily manage multiple processes so let's start and let's quickly recap from the last video how to create and start processes so we say from multi-processing import process and then down here we define a number of processes so a typical good choice for this is the number of processes on your machine and you get this with OS dot CP you count and then you create your different processes with proces equals process and this takes a target is a function argument and it the target is a callable function this is that is then executed by this process so we define a function up here this simply square some numbers and then we give this to our process here and then for each process we call process dot start and also process dot join so this says says that we want to wait for all processes to finish and block the main program until all these processes are done so this is all we need to setup multiprocessing and now let's go into more detail and first let's talk about how we can share data between processes so in the last video with multi-threading we learned that we can easily share data between threads with a global variable and now with processes processes don't live in the same memory space so they don't have access to the same public data and because of that they need special share memory objects to share data and there are two shared memory objects that we can use we can use a value for a single value or we can use an array so we say from multiprocessing import value and import array and down here let's first start with a single shared value so we say shared number and this is now a value and this takes two arguments first we have to give it the data type as a string so we give it an eye for integer and a starting value this is now 0 and first of all print this so we say number at beginning is and then we say we access the shared number with or this value with shared number dot value so now if we run this and we see that this is 0 and now let's create two processes that should modify this number so we say process 1 equals process and as a target it gets a function that we call at 100 so let's define this up here so let's define at 100 and this gets a number and then it should modify this number a couple of times so we say for I in range 100 so 100 times it should say number dot value plus equals 1 so it should increase this by 1 and we also want to modify a behavior that takes some time so we say time dot sleep and 0.01 and so here we give it this at 102 hour process and it also needs arguments so we say arts equals and this is a tuple so here we give it the shared number and be careful since this is a tuple with one element it also needs a comma here so that Python knows that this is a tuple and then we create a second process that will do the same thing so proces to that should add 100 to our shared variable and then we say process 1 dot start and process 2 dot start and then we wait for them to complete so process 1 dot join and process to the join and now at the end we again print our number so we say number at and is and then access it with shared number dot value so let's execute this and see what happens so the beginning is 100 it's zero and now we got lucky now let's run this a second time and now it's not 200 so it's only 168 and why is that because here a race condition happened and I will not explain this in details so please have a look at the previous video there I explained in detail how race conditions occur so a race condition occurs when two threats or processes try to access and modify the same shared variable at the same time so in this case both processes try to read and write them into this object at the same time so some operations might get lost here so in this case it's only 168 and to prevent this we must use a lock so we say from multi-processing import lock and I also talked about this in the last video so please check this out so a lock prevents another process from accessing this at the same time so to use this we create a lock object so we say lock equals lock and then we give this to our function to to our function yes so this now also takes a lock and a lock has two important methods so first we say lock dot acquire and then at the end we say lock don't release so as soon as we say lock dot acquire it will set this in a locked state so this means that while this is running no other processes access to this code and can execute this part here and then when we unlock the state again with lock that released and the second process can also execute this so this is all we need to prevent multiple process to modify this at the same time so now let's run this and now we get 200 let's run this again and yeah still working and the better way to use locks is to use locks as a context manager so whenever you say lock acquire you always have to call locked release then at the end otherwise this will lock and your program cannot continue so don't forget this and you can use a lock as a context manager so we say with lock : and then your code so this automatically will acquire and release this for you so yeah this also works and now this is how we can share a single value and now let's share a array so we say shared array equals arrey and this also needs a datatype so in this case let's give it a D for double and here give it a list as initial values so we save put in zero point zero here 100 point zero and 200 point zero and then we say our array at the beginning is and let's say shared array and then we have to access each element with inside brackets and then with the index and we can also use slicing here to access all in the indices so let's just put in a colon here and then again print this at the end so at the end we want to print our array so array at the end is this and now we have to change our function so this now takes multiple elements that this takes numbers and then we have to go over each number here and increase it so first of all let's also change this parameter here so let's say shared array and now in our you know function what we want to do is we want to go over each number and increase it but be careful here so we cannot say for number in numbers and then simply say number plus equals one so now if we run this this will print our array at the beginning and at the end and this is still the same and this is because this loop here will create a local variable called number that is then in so this is nothing to do with our shared value object so in order to do this we have to say four and let's say for I in range and then the range has the length of our array and then we access each element with numbers dot I and say plus equals one and now let's run this and now we see that it got modified but we also had race conditions here so don't forget the lock so we say with lock and then our modification operation and now we increased each element in our array by 200 so this is how we can use the shared value and shared array and we can also use a queue to exchange elements between processes and I also already show this in the last video so a queue can be used for process safe data exchanges and so in the last video we set from queue import queue and there we have to use a slightly drift different queue that is formed from the multi processing module so this has all the same methods and except the the task done and the task and the join method so a queue is a linear data structure that follows the first-in-first-out principle so the first element that you put into your queue that is then also the first element that gets retrieved when you want to get elements so let's make an example to use a queue and exchange data between multiple processes so in this case let's say Q equals Q and then create two processes that should do should access and write to this Q so we we have a process that gets as a target it gets a function that we call Square and as arcs it gets some numbers and our Q and then we create a second process that has a second different function here so we call this make- and it has the same arguments so it will write the same q and now let's define our functions here so we say def Square and this will get some numbers and a Q and then we say so in this case for I in numbers we calculate the square and put it into our Q with Q dot put I times I and then a second function make- and there we also it also takes some numbers and aq and here we also go over our numbers for I in numbers and then we say Q dot put minus one times I and then let's say and let's start our processes so process 1 dot start and process to the start and then process 1 dot join and process to the join and here we don't have to call queued up join because there is no method cue the but what we can do is we say while our queue is not empty so while not Q dot empty and then we want to print each element so print Q dot get so this will return and also remove the first element in our queue and yeah now let's run this and see what happens numbers is not defined I'm sorry I have to create in numbers variable and I will say this is a range object from 1 to 5 so or 5 should be included so I say from 1 to 6 and now this will print each element and we see that both processes have access to this queue and can write and put elements into it and then in our main process we can also access the queue and get the elements back so this is how we can a can use a queue and now as a last thing let's talk about a process pool so a process pool can be used to manage multiple processes so a process pool object controls a pool of worker processes to which shops can be submitted and it can manage the available processes for you and split for example data into smaller chunks which can then be processed in parallel by different processes so let's have an example how this works basically a pool takes care of a lot of things for you so you don't have to consider a lot so we simply say from processing import pool and then down here we create a pool and we say so we say pool equals pour and then it has two or four nets and it has four important methods that you have to know and for the rest I would recommend to have a look at the documentation because there are a lot of more methods but the most important ones are map apply join and close so what we want to do is we want to create multiple process that should access a or execute a function so we call define a function cube and this takes a number and returns the cube so it little will return number times number times number and now here we can simply say or we create some numbers so numbers equals a range object from zero to ten or ten or only to nine and then we say pool dot map and now we map and we have to give it a function so we give it the cube and the numbers and this will return a result that we can then print so print our result but first of all we have to so this will what this will do this will automatically uhm allocate them the maximum number of available processors for you and create different processes so typically this will create as many processes as you have a course on your machine and then it will split this iterable into an equal into equal sized chunks and submit this to this function and this function is now executed in parallel by different processors by different processes so this is all you need to write and then the pool will take care of the rest so this will allocate the pools it will split the data and then run this method in parallel and when it is done it will return the result and we have to call pool dot close and then we can call pool the join so this means that we want to wait for the pool to to process all the calculations and return the results and we have to remember that we should call pool that close before so now if you run this we can print our result and we can see that it has the cube here so yeah this is how we can easily use the the pool to to run different processes with a function and if we simply want to have one function executed by a pool then we can say poor dot apply and then also the cube and then in this case it will only has one number here so we can for example say apply numbers the first element so number zero so this will execute a process with this function with one argument and you so this is the most important things about pools and there are also asynchronous calls to this map and apply functions but I will not cover them here and yeah for now that's all I wanted to show you about the multi processing module I hope you enjoyed this tutorial and now feel comfortable working with multiple threats and processes and see you in the next tutorial where we will learn about function arguments and parameter passing in Python

Original Description

In this Python Advanced Tutorial, I will go into more detail about the multiprocessing module in Python. This video will cover: - How to create and run multiple processes - How to share data between processes - Hot to use Locks to prevent race conditions - How to use a Queue for process safe data exchanges - How to use a Process Pool to manage multiple worker processes ~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~ ✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer * 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN A written Tutorial can be found here: https://www.python-engineer.com/courses/advancedpython/17-multiprocessing/ You can find me here: Website: https://www.python-engineer.com Twitter: https://twitter.com/patloeber GitHub: https://github.com/patrickloeber #Python ---------------------------------------------------------------------------------------------------------- * This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 17 of 60

1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This video tutorial covers advanced topics in Python's multiprocessing module, including parallel processing, shared memory, locks, and queues. It provides examples and code snippets to demonstrate the concepts, making it a practical resource for intermediate learners.

Key Takeaways
  1. Create and start multiple processes
  2. Share data between processes using shared memory objects
  3. Use locks to prevent race conditions
  4. Use queues to manage data between processes
  5. Create a process pool to manage multiple processes
  6. Split data into smaller chunks for parallel processing
  7. Use map, apply, join, and close methods of a process pool object
  8. Execute a function in parallel using pool.map
💡 The multiprocessing module in Python provides a powerful way to achieve parallel processing, but it requires careful management of shared resources and synchronization to prevent race conditions.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →