Can some please assist me with the my code below, I'm trying to implement a generator with multiple yields using multiprocessing.
I would normally use a next
followed by a send
for the second and third calls to the generator but I'm not sure how this is done using the multiprocessing module.
from multiprocessing import Pool
def sq_divide(x, y):
yield x**y
send_receive = (yield x) #"send_receive" is used as confirmation that text "Done!!" was received
if send_receive=="Done!!":
yield x / y
if __name__ == "__main__":
p = Pool(4)
#first generator call
n = p.map(sq_divide, (range(1, 10), range(1, 10)))
print(n)
#second generator call
n = p.map(next, n)
#last generator call
n = p.map(n.send, "Done!!")
print(n)
My expect output is:
first print:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
second print:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
third print:
[1, 1, 1, 1, 1, 1, 1, 1, 1]