0

I want to get return values from multiple processes initialised in one function and started in another function.

 import multiprocessing
 import time

 class Auto:

    def __init__(self):
        self.msf = 0

    def auto(self, return_dict, i):
        # print "hello"
        return_dict["hello"] = "hello{}".format(i)

    def msf1(self):
        man = multiprocessing.Manager()
        self.return_dict = man.dict()
        self.a= multiprocessing.Process(target=self.auto, args=(self.return_dict, 1, ))
        self.b= multiprocessing.Process(target=self.auto, args=(self.return_dict, 1, ))
        self.c= multiprocessing.Process(target=self.auto, args=(self.return_dict, 1, ))

    def msf2(self):
        self.a.start()
        self.b.start()
        self.c.start()

        return self.return_dict.values()
2
  • You could return them via a queue Commented May 22, 2019 at 13:08
  • Can you illustrate with code? Commented May 23, 2019 at 7:07

1 Answer 1

1

You can use a Queue() to collect items from multiple processes. [docs]

Here is a very simple example of how it can work. See this part of the docs for a more in depth example of how it works.

def number(done_queue):
    done_queue.put(5)

done_queue = multiprocessing.Queue()
x = Process(target=number, args=(done_queue))
x.start()
x.join()
y = [i for i in done_queue]
print(y)
Sign up to request clarification or add additional context in comments.

Comments

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.