How To Use Multiprocessing In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone thank you for joining again my name is Taylor and today I'm going to teach you about multiprocessing in Python multiprocessing allows us to spawn new processes and utilize parallel programming without the restrictions of the global interpreter lock also known as the Gill so first what we want to do is create a python file I've already created my python file here and I have my terminal open on the bottom I'm I'm using Kate as our text editor today so what we want to do first is import the multiprocessing module we'll say import space multi-processing space as MP so what this is saying is import the multiprocessing module and give it the variable name MP so now let's hit enter a few times and let's create a function called area so we'll say de f space area open and close parentheses in those parentheses let's send in L comma W for length and width so let's add a colon at the end and let's say print L * W and let's hit enter a few times let's create our main function so we'll say if uncore uncore name uncore underscore double equals open quotations underscore uncore main underscore underscore add a colon at the end now let's spawn our first process so the way we do that is we'll give this a variable name we'll say P1 for process one is equal to MP because this is our variable name for multi-processing do capital P process open and close parentheses and in those parentheses we need to specify a Target and since we're using our arguments we also need to specify arguments in these parentheses we need to specify the target which will be our function area so we have to say Target is equal to area comma now we need to specify our arguments which is L comma W so two arguments so we'll say args is equal to open and close parentheses in those parentheses we need to send in numerical values so we'll say 10 is the length and 50 is the width now what we want to do is start this process the way we do that is calling p1. start open and close parentheses hit enter and finally we need to join this process with all our other processes the way we do that is calling the variable name p1. jooin open and close parentheses now let's save this and type py Python and then the name of our file and hit enter as you can see it spawned a process and it joined the process with all our other processes it calculated the length times width and finally it printed out our results here so now what if we wanted to create and run multiple processes simultaneously well we could do that with the multiprocessing module let me show you how so we're going to create another function outside of our main function so we'll say DF space and we'll call this hello open and close parentheses this will not take any arguments at a colon at the end and we'll just simply say print hello let's save this let's go into our main function now we're going to follow the same procedure as we did with P1 so process one so this will be process 2 P2 is equal to MP do capital P process open and close parentheses in those parentheses we're going to specify a Target which is going to be hello and add a comma at the end finally we want to start this process before we join it so we can say P2 Dost start open and close parentheses and then we need to join this process too so we do P2 dojin Jo open and close parenthesis let's save this and let's rerun the program as you can see it spawned two processes and it calculated the length times width here and it also printed out hello so this is just the basics on how you would utilize multi-processing to spawn multiple processes simultanously thank you all for watching I invite you to subscribe and I hope to see you next time
Info
Channel: Taylor's Software
Views: 384
Rating: undefined out of 5
Keywords:
Id: xwdGmFoL6do
Channel Id: undefined
Length: 4min 59sec (299 seconds)
Published: Sun Jul 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.