anderish anderish - 11 months ago 32
Python Question

Python Using Multiprocessing

I am trying to use multiprocessing in python 3.6. I have a

for loop
that runs a
with different arguments. Currently, it is running one at a time which is taking quite a bit of time so I am trying to use multiprocessing. Here is what I have:

def test(self):
for key, value in dict.items():
pool = Pool(processes=(cpu_count() - 1))
pool.apply_async(self.thread_process, args=(key,value))

def thread_process(self, key, value):
# self.__init__()
print("For", key)

I think what my code is using 3 processes to run one
but I would like to run 1 method per process but I don't know how this is done. I am using 4 cores btw.

Answer Source

You're making a pool at every iteration of the for loop. Make a pool beforehand, apply the processes you'd like to run in multiprocessing, and then join them:

from multiprocessing import Pool, cpu_count
import time

def t():
    # Make a dummy dictionary
    d = {k: k**2 for k in range(10)}

    pool = Pool(processes=(cpu_count() - 1))

    for key, value in d.items():
        pool.apply_async(thread_process, args=(key, value))


def thread_process(key, value):
    time.sleep(0.1)  # Simulate a process taking some time to complete
    print("For", key, value)

if __name__ == '__main__':
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download