Python Tutorial - 28. Sharing Data Between Processes Using Array and Value

codebasics · Beginner ·⚡ Algorithms & Data Structures ·9y ago

Key Takeaways

Demonstrates sharing data between processes using Array and Value in Python

Full Transcript

dear friends welcome to core basics coding tutorial in this tutorial we are going to learn how to share data between two processes in Python now if you have gone through my multiprocessing introduction tutorial or you have already seen a problem when you try to share a global variable between two processes I'm going to revisit the same problem here once again so in this program I have a list of numbers and I am creating a new process here which is just calculating the square of all those numbers and it is putting the result back into this result global variable so you can see here I have a global result and it is referring to this variable ok and once I'm done with my processing I am printing the value of that variable inside my process and also outside the process okay now when I run my program what happens is inside the process values look ok so I have 2 3 5 their Square number is 4 9 25 pretty good but outside the process when I try to print the same exact variable I see blank now why is that so if you remember from our introduction tutorial whenever you create a new process the new process will get its own address space at a space meaning the space where it stores all the variables etc so what happens is as you can see in the diagram on the left hand side I have a parent process which has a result global variable but when you create a child process which is shown on the right hand side the memory for this global variable will be copied so now I have a separate copy of result variable hence when you when you run your calculated square function it is only updating the copy in the child process okay so this is the problem now how to solve this issue how to solve this issue of share data between two processes well there is a way the way is to use shared memory so as you can see in the diagram when you use shared memory the memory lives outside the processes hands you can access it and it will bring the result for you so let's use that memory here in our program now there are two ways of using shared memory one is value another is array so in this program I'm going to use array first and then I'll show you how to use value so first let's create a shared memory variable now share memory variable is different than your regular variable so you have to use multiprocessing dot array here now here the first thing is you specify your data type so my data type here is integer okay and then you specify your size so my size is three because I had three numbers here so my result is gonna have three and then I will pass down this shared memory into my process okay so my process has one more variable called result so let me remove this global variable because it's anyway not working now shared memory has different set of methods so a pen is not part of the method list so happen is not gonna work okay so we have to use a simple index now to use simple index what you do this or ID X and in numerator so when you use this function you will always get the index and value both okay so here I'm just saying result is equal to and just cross an okay and I'm removing this here after that I'm just printing the result here all right let's run this okay here it's not printing it okay so we have to use a different method so we'll just say okay so here I'm going to say result like this this is a way to print all the elements in an array okay excellent so now you can see that we are printing it outside the process and it still print out the result fine so you can use array this way to share data between two processes so it's solve our problem now these two processes can exchange data using this sad memory variable called result okay all right let's go over value now so for value so value similar to array but it's just a single value okay so you can create value like this I'm going to create a double variable on shared memory and assign a value 0.0 and when I pass this to my child process here I'm going to receive it as an argument in this function and here I will say value dot value maybe we can just change this variable name to be V I'm going to just assign some arbitrary value okay and here you can print that value we dot value so as you can see that in our child in our parent process we are creating a value variable on shared memory we are passing that to a different process here which is setting the value and then back to our parent process it is printing the value here now when you run this see it's working basically the child process is updating the value and still the parent process is able to access the same value okay so this was all about values and array there are in fact other ways of sharing data which is queue and pipe but we will cover those in the future tutorials ok thank you for watching

Original Description

Today’s tutorial is based on sharing data between processes using Array and Value. The topics that we are including in this python tutorial are how to solve issue sharing data between the process, what is shared memory, how to use shared memory in the process using an array, what is shared memory methods enumerate() and how to use shared memory in a process using value. Topics that are covered in this Python Video: 0:00 Overview 2:15 How to solve issue sharing data between the process 2:21 What is shared memory 2:41 Use shared memory in a process using an array 3:37 Shared memory methods enumerate() 5:15 Use shared memory in the process using the value 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_value_array.py Next Video: Python Tutorial - 29. Sharing Data Between Processes Using Queue: https://www.youtube.com/watch?v=sp7EhjLkFY4&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=32 Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 54 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
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

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python

Chapters (6)

Overview
2:15 How to solve issue sharing data between the process
2:21 What is shared memory
2:41 Use shared memory in a process using an array
3:37 Shared memory methods enumerate()
5:15 Use shared memory in the process using the value
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →