I have the question on multiprocess
in python3.5
.
If i have two list like:
xlist = [1,2,3]
ylist = [4,5,6]
and I want to do :
for i in xlist:
for j in ylist:
print (i*j)
the output is
4
5
6
8
10
12
12
15
18
I try to do that like this with Multiprocess:
import multiprocessing
global xlist
xlist = [1,2,3]
ylist = [4,5,6]
def product(ylist):
for x in xlist:
for y in ylist:
print (x,y)
return 'OK'
if __name__ == "__main__":
pool = multiprocessing.Pool()
results = []
for i in range(0, len(ylist)):
result = pool.apply_async(job, args=(ylist,))
results.append(result)
# print (result.get())
pool.close()
pool.join()
for result in results:
print(result.get())
But I can not got the output show above. My output will be
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
1 4
1 5
...
with the code.
Are there any ways to achieve the goal (must use multiprocess)?
I think you want to try a simple example before using it on a very big set of numbers, and with a more complex function.
Here is one program that prints what you want, uses multiprocessing, and should scale for larger lists and more complex functions.
import multiprocessing
xlist=[1,2,3]
ylist=[4,5,6]
def enum_tasks():
for x in xlist:
for y in ylist:
yield (x,y)
def product(xy):
x,y = xy
return x * y
if __name__ == '__main__':
CHUNK_SIZE = multiprocessing.cpu_count()
pool = multiprocessing.Pool()
for result in pool.imap(product, enum_tasks(), CHUNK_SIZE):
print result