Python multiprocessing Process ID

Question:

I’m using multiprocessing.Pool too run different processes (e.g. 4 processes) and I need to ID each process so I can do different things in each process.

As I have the pool running inside a while loop, for the first iteration I can know the ID of each process, however for the second and more iterations this ID changes or at least I can’t find one property that seems to be the same for each process in all iterations.

The relevant part of the code is as follows:

     while i <= maxiter:
        print('n' + 'Iteration: %r'%i + 'n')

        pool = mp.Pool(processes = numprocs)

        swarm = pool.map_async(partial(proxy, costf = costFunc, i=i),Swarm)
        pool.close()
        pool.join()

        Swarm = swarm.get()

I’ve tried with the following properties to properly ID the processes but it’s not working for me:

print(mp.Process().name)
print(mp.current_process().name)

With this the output is:

Iteration: 1

Process-2:1
Process-1:1
ForkPoolWorker-1
ForkPoolWorker-2
Process-3:1
ForkPoolWorker-3
Process-2:2
ForkPoolWorker-2
Process-3:2
Process-2:3
ForkPoolWorker-3
ForkPoolWorker-2
Process-1:2
ForkPoolWorker-1
Process-4:1
Process-3:3
ForkPoolWorker-4
ForkPoolWorker-3
Process-2:4
ForkPoolWorker-2

Iteration: 2

Process-5:1
ForkPoolWorker-5
Process-5:2
Process-7:1
ForkPoolWorker-7
Process-6:1
ForkPoolWorker-5
ForkPoolWorker-6
Process-5:3
ForkPoolWorker-5
Process-7:2
ForkPoolWorker-7
Process-5:4
ForkPoolWorker-5
Process-6:2
ForkPoolWorker-6
Process-7:3
ForkPoolWorker-7
Process-8:1
ForkPoolWorker-8

Any ideas how can I ID each process the same way every time?

EDIT 1:

I’ve simplified the program to this but the idea is the same:

import random, numpy as np,time
import multiprocessing as mp

def costFunc(i):
    print(mp.current_process().name,mp.Process().name)
    return i*1

class PSO():
    def __init__(self,maxiter,numprocs):

        # Begin optimization Loop
        i = 1
        self.Evol = []

        while i <= maxiter:
            print('n' + 'Iteration: %r'%i + 'n')
            pool = mp.Pool(processes = numprocs)
            swarm = pool.map_async(costFunc,(i,))
            pool.close()
            pool.join()

            Swarm = swarm.get()

            i += 1

if __name__ == "__main__":
    #mp.set_start_method('spawn')
    PSO(10,1)

OUTPUT:

Iteration: 1
ForkPoolWorker-1 Process-1:1
Iteration: 2
ForkPoolWorker-2 Process-2:1
Iteration: 3
ForkPoolWorker-3 Process-3:1
Iteration: 4
ForkPoolWorker-4 Process-4:1
Iteration: 5
ForkPoolWorker-5 Process-5:1
Iteration: 6
ForkPoolWorker-6 Process-6:1
Iteration: 7
ForkPoolWorker-7 Process-7:1
Iteration: 8
ForkPoolWorker-8 Process-8:1
Iteration: 9
ForkPoolWorker-9 Process-9:1
Iteration: 10
ForkPoolWorker-10 Process-10:1
Asked By: Miguel Oliveira

||

Answers:

You are creating a new pool in each iteration of the loop, so processes in the pool are never re-used.

Move pool = mp.Pool(processes = numprocs) (and pool.close() and pool.join()) out of the while loop to re-use processes in the pool.

Answered By: Erik Cederstrand
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.