1

I'm trying to use a generic object called 'Memory' to temporary store a value that is writen by a process called 'Writer_process' and readed by 'Reader_process' but reader return only the original value stored in 'Memory'.

import multiprocessing
import time

class Writer_process(multiprocessing.Process):
    def __init__(self, val, memory):
        super().__init__()
        self.val = val
        self.memory = memory

    def run(self):
        cont = 0
        while True:
            cont += self.val
            self.memory.num = cont
            time.sleep(1)

class Reader_process(multiprocessing.Process):
    def __init__(self, val, memory):
        super().__init__()
        self.val = val
        self.memory = memory

    def run(self):
        while True:
            print(self.memory.num)
            time.sleep(self.val)

class Memory():
    num = 0

if __name__ == '__main__':
    memory = Memory()
    writer = Writer_process(1, memory)
    reader = Reader_process(1, memory)
    writer.start()
    reader.start()
1
  • If you need real shared mutable state in multiprocessing, you will have to use low-level primitives. It is really best to avoid shared mutable state in multiprocessing, though. By the way, judging by the names of your objects, what you want is an IO-bound reader/writer-pair. In that case it's best to use asynchronous execution instead of multiprocessing. Commented Sep 27, 2018 at 23:31

1 Answer 1

1

Thank you to Eli Korvigo I solved the problem using a 'Manager' to manage the access to the memory and a specific multiprocessing object called 'Value' can allow me to store a single data and share it betwneen different processes.

import multiprocessing
import time

class Writer_process(multiprocessing.Process):
    def __init__(self, val, memory):
        super().__init__()
        self.val = val
        self.memory = memory

    def run(self):
        cont = 0
        while True:
            cont += 1
            self.memory.value = cont
            time.sleep(self.val)

class Reader_process(multiprocessing.Process):
    def __init__(self, val, memory):
        super().__init__()
        self.val = val
        self.memory = memory

    def run(self):
        while True:
            self.datum = self.memory.value
            print(self.datum)
            time.sleep(self.val)

if __name__ == '__main__':
    with multiprocessing.Manager() as manager:
        memory = manager.Value('i', 0)
        writer = Writer_process(1, memory)
        reader = Reader_process(1, memory)
        writer.start()
        reader.start()
        writer.join()
        reader.join()
Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad you've solved your issue. You can now accept your own answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.