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.