Learning is a mess
Learning is a mess

Reputation: 8277

Python multiprocessing: knowing the thread/CPU number

I'm trying to code a parallel code in Python using the multiprocessing module and I would like to know of a way to locally know which CPU is computing, but I only know of multiprocessing.CPU_count() to know the total CPU cores.

I'm looking for an equivalent of:

omp_get_thread_num()

in C++ openMP.

Is there such a method in Python.multiprocessing?

Upvotes: 2

Views: 10106

Answers (1)

MestreLion
MestreLion

Reputation: 13686

It's not trivial to retrieve which CPU a process is running at (if possible at all), but if you:

  • Start the same number of processes as there are CPUs available, as reported by multiprocessing.CPU_count(), as most applications do;

  • Assume that each process will run in a distinct CPU core, as expected when using multiprocessing module;

Then you can "cheat" and give each process a unique name that will identify its CPU core! :)

for i in xrange(multiprocessing.CPU_count()):
    mp = multiprocessing.Process(target=foo, args=(bar,), name=i).start()

And then retrieve it inside the worker function spawned in the subprocess:

print "I'm running on CPU #%s" % multiprocessing.current_process().name

From the official documentation:

multiprocessing.current_process().name

The process’s name.

The name is a string used for identification purposes only. It has no semantics.
Multiple processes may be given the same name.
The initial name is set by the constructor.

Upvotes: 7

Related Questions