Python Tutorial - 30. Multiprocessing Lock

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

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

Playlist

Uploads from codebasics · codebasics · 57 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
53 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
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 teaches the basics of multiprocessing locks in Python, including how to create and use locks to protect shared resources and ensure data consistency.

Key Takeaways
  1. Create a lock variable using the multiprocessing module
  2. Pass the lock to multiple processes
  3. Use the acquire and release methods to lock and unlock the critical section
  4. Ensure the critical section is properly synchronized
💡 Using locks is crucial in multiprocessing to prevent data inconsistencies and ensure the integrity of shared resources.

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?
Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →