I am using multiprocessing from Python for testing purpose and there are somethings I don't understand.
A priori, processes have their own memory space so we can't share and Python class between them.
But look at my code:
import sys,
import time
from multiprocessing import Queue, Process
class MainClass():
def __init__(self, q):
self.q = q
print("Queue in Main", q)
def start_p(self):
p = Proc(self.q)
p.processing()
def run_p(self):
p = Process(target=self.start_p, args=())
p.start()
return p
class Proc():
def __init__(self, q):
self.q = q
def processing(self):
print("Queue in process", self.q)
n = ''
try:
n = self.q.get(0) # Get first item in the "queue"
except KeyError as e:
print("NOK", e)
print("GET: ", n)
print('Size: ', self.q.qsize())
if __name__ == "__main__":
# Creating a queue
q = Queue()
# Add 10 numbers in the queue
for i in range(0,10):
q.put(i)
# Add the queue to Main Class
s = MainClass(q)
print("Initial queue size", q.qsize())
# Starting 3 process
p1 = s.run_p()
p2 = s.run_p()
p3 = s.run_p()
#time.sleep(2)
print("Final queue size", q.qsize())
I have created a queue on main process, with 10 numbers. Then, I ran 3 process so each one run a task consting of just getting (and delete) first item in a queue.
What I misunderstand is how can this program work and return a final queue 7 ? It seems the queue is shared...but the object itself (multiprocessing) is located in different memory place... But there is no "pointer" mecanism in python ?
The result when I run the programm below:
Behaviour is almost the same on linux except the memory adress is the same for all instances.
Please can someone explain me ?

