Jahaha
Jahaha

Reputation: 29

Create Process for Each Item of a List

I would like to create an instance of a process for each item of a list. The process should only be created one at a time, a new process should only be created after the one before it has completed. How do I do this?

Below code does not work, it only prints the list of IPs.

def performwork(IP):
    .................
    .................


for IP in listOfIPs:
    print IP
    multiprocessing.Process(target = performwork, args=(IP)) 

Upvotes: 1

Views: 298

Answers (1)

Infoblack
Infoblack

Reputation: 51

from contextlib import closing
from multiprocessing import Pool

with closing(Pool(processes=1)) as pool:
    pool.map(performwork, listOfIPs)

Upvotes: 1

Related Questions