Python: Multicore processing? -
I'm reading about Python. I still do not think I have a good understanding of what this can do.
Let's say I have quad core processor and I have a list with 1,000,000 integers and I want the sum of all the integers. I could just do this:
list_sum = sum (my_list)
but it only sends it in one core.
Is it possible, using multiprocessing module to divide the array, and each core gets the sum of its fraction and returns the value so that the total amount can be calculated?
Something like this:
core 1_sum = sum (my_list [0: 500000]) # 1 core for core 2_sum = sum (my_list [500001: 1000000]) # For # 2 core # all_core_sum = core1_sum + core2_sum #core 3 calculates the final
Any help would be appreciated.
Yes, it is possible to do more in conjunction with multiple processes, doing so with lots of threads Q}: do_sum (q, l): q.put (sum (l)) def main (): My_list = range (1000000) q = line (), with the multiprocessing import process:
P1 = Process (target = do_sum, args = (q, my_list [: 500000])) p2 = process (target = do_sum, ar gs = (q, my_list [500000:])) p1.start () p2.start () R1 = q.get () r2 = q.get () print r1 + r2 if __name___ == '__ main__': main ()
However, it is possible that many The process It is slow to compare with doing the same process, because the copy of the data and the back of the copy are more expensive than being told immediately.
Comments
Post a Comment