1

I have seen this example here on the website and I am wondering how to make the code work in accordance with the pool function. I have been through a lot of documentation but the closest to this code is the question asked by Olson: Can I use a multiprocessing Queue in a function called by Pool.imap?

I would like to assign 4 processes to the code. Please be gentle, I have been going through stackoverflow questions as well and all I am asking is that someone provides an example in accordance with the example I provide. Here is the code:

import random
from multiprocessing import Process, Queue
import time
from datetime import datetime

def function1(q):
    while True:
        daydate = datetime.now()
        number = random.randrange(1, 215)
        print('Sent to function2: ({}, {})'.format(daydate, number))
        q.put((daydate, number))
        time.sleep(2)


def function2(q):
    while True:
        date, number = q.get()
        print("Recevied values from function1: ({}, {})".format(date, number))
        time.sleep(2)

if __name__ == "__main__":
    q = Queue()
    a = Process(target=function1, args=(q,))
    a.start()
    b = Process(target=function2, args=(q,))
    b.start()
    a.join()
    b.join()

I am new here so please be gentle thank you. So how can I use pool function with this code and assign 4 processes to it? That is the question and I thank you all very much in advance.

Bondeaux
  • 174
  • 1
  • 3
  • 10
  • `multiprocessing.Pool` isn't something that you would use with the above code. A Pool is generally used to process a list of data in parallel and then exit. In the example above you're trying to get 2 functions to run in parallel indefinitely. For this `Process` is the way to go. If you can give more details on what your trying to achieve maybe we can point you to a better example. – bivouac0 Jan 19 '18 at 14:57
  • As I stated above, an example where you apply pool to the code is what I am looking for. If you know how, pleas apply. I mean, I just want to add 4 processes to the task. – Joemoreneau Jan 19 '18 at 15:05
  • I just want to add 4 processes to the task. If that is possible through another function, feel free to apply! – Joemoreneau Jan 19 '18 at 15:29

0 Answers0