Python Tutorial - 30. Multiprocessing Lock
Key Takeaways
This video tutorial covers the concept of multiprocessing locks in Python, including why locks are needed, how to create and use lock variables, and the importance of critical sections in protecting shared resources.
Full Transcript
dear friends welcome to coal basics coding tutorial I hope you are having great time learning multiprocessing and python in this tutorial we are going to cover one very interesting topic called log now if you have taken any computer science classes or any operating system class you you have already learned about Locke Locke is a very important concept when it comes to multi processing and operating system concepts okay so let's go over this log concept first of all let's think about why Locke is needed in our real life in our day to day life there are resources which cannot be accessed by two people at the same time for example bathroom ok bathroom door have a lock why because if two people try to access it at the same time it's really gonna create a pretty embarrassing situation isn't it so that's why we protect the bathroom which is a shared shared resource with a lock similarly in programming world whenever two processes or threads are trying to access a shared resource such as a memory shared memory file or a database it can create a problem so you need to protect that access with a lock what happens if you don't add that protection I'm going to show that by running this goal so this program is a banking software program and here I have two processes you can see them here the first process is depositing money into a bank and the second process is withdrawing money from the bank and in the end I am printing the final balance okay so I'm starting with a two hundred dollar balance in deposit section I am depositing $100 so I have a for loop which I traced through 100 times and in each iteration it will add one dollar to my bank account and similarly in the withdrawal I have same loop which I trust 100 times and every time it will deduct one dollar for my from my bank account so in the end I should have $200 left here okay now I'm using a shared memory variable called value so if you remember from my previous tutorial this multiprocessing value is a shared memory resource let's see what happens when you try to run this program okay it printed $200 fine so sometimes it will work but sometimes it will print inconsistent results so I'm going to run it multiple times this time it printed 205 [Music] 201 so you can see that every time it's just printing different values now why does this happen this happens primarily because when this process tries to read this variable called balanced lot value which is in shared memory let's say this has a $200 value it will read it and then it will add one and then it will put back the same thing into a same variable so let's say this is 200 and it is doing this addition operation at operating system level so at operating system level it will be executing multiple assembly-line instructions okay so let's say it we have read the variable 200 it added one and it's gonna write back 201 here now while it is doing it at that exact same time this instruction also got executed so although I am depositing first and then withdrawing what happened is when I try to read this it will be still 200 because this guy has not written back to the original variable okay so instead of reading this as 201 it will read this as 200 and it will have one time 199 as a balanced-load value instead of 200 so that's why you are getting this inconsistent behavior alright so now let's use log to log the axis okay so what you are going to do is create a log variable and use multi processing modules log class this is how you create a lock multi processing dot clock okay and pass that log to both of these processes okay and now here I am going to say lock dot acquired so to put a lock you just call lock not acquire function and then to release a law it's a release function so it's pretty intuitive and straightforward okay so lock dot acquire and then lock dot release now this section of food is called critical section whatever part of your code is accessing the shared resource which you want to protect that part is called critical section so you always have to put locked or acquire a lock dot release around that critical section okay now let's run it okay so now it's gonna print 200 every time so I'm running it multiple times just to prove that I am NOT making this up it now works okay so the guys who write your banking software they better better used lock otherwise you will have cows in your bank account injections you know so using lock is very very important okay so that was all about locks we are going to cover a few more multi processing concepts in future tutorials multi processing learning multi processing with python is extremely easy and i hope all of you are having great time and I am pretty confident all of you can done Python very easily by following this tutorials so thank you for watching and I'll see you in the next tutorial bye
Original Description
In this we are having a look on how multiprocessing lock works in python. We will gain an understanding of what is lock, why lock is needed with the help of examples, how to create and use lock variable, what is lock(), acquire() and release () function and what is a critical section.
Topics that are covered in this Python Video:
0:00 Overview
0:11 What is lock? why lock is needed?
3:36 Example of lock
4:44 Create and use lock variable and lock(), acquire() and release() function
5:36 What is critical section?
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_lock.py
Next Video:
Python Tutorial - 31. Multiprocessing Pool (Map Reduce): https://www.youtube.com/watch?v=_1ZwkCY9wxk&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=34
Website: https://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Playlist
Uploads from codebasics · codebasics · 57 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
53
54
55
56
▶
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 (5)
Overview
0:11
What is lock? why lock is needed?
3:36
Example of lock
4:44
Create and use lock variable and lock(), acquire() and release() function
5:36
What is critical section?
🎓
Tutor Explanation
DeepCamp AI