Skip to content
Related Articles

Related Articles

Improve Article

How to get the process id from Python Multiprocess?

  • Difficulty Level : Medium
  • Last Updated : 27 Mar, 2021

In this article, we will see how to get the process id from Python Multiprocess For this we should make use of method multiprocessing.current_process() to get the multiprocess id.  Multiprocessing refers to the ability of a system to support more than one processor at the same time. Applications in a multiprocessing system are broken into smaller routines that run independently. The operating system allocates these threads to the processors improving the performance of the system.

Consider a computer system with a single processor. If it is assigned several processes at the same time, it will have to interrupt each task and switch briefly to another, to keep all the processes going.
This situation is just like a chef working in a kitchen alone. He has to do several tasks like baking, stirring, kneading dough, etc.

Example 1:

First, we need to import a multiprocessing library in python.


# importing library
import multiprocessing
# define function
def twos_multiple(y):
    # get current process
    return y * 2
pro = multiprocessing.Pool()
print(, range(10)))


Example 2:

Multiprocessing will maintain an itertools.counter object for each and every process, which is used to generate an _identity tuple for any child processes it spawns and the top-level process produces child process with single-value ids, and they spawn process with two-value ids, and so on. Then, if no names are passed to the Process constructor, it simply autogenerates the name based on the _identity, using  ‘:’.join(…). Then Pool alters the name of the process using replace, leaving the autogenerated id the same.

The auto-generated names are unique. It will return the process object itself, there is a possibility of the process being its own identity.

The upshot of all this is that although two Processes may have the same name, because you may assign the same name to them when you create them, they are unique if you don’t touch the name parameter. Also, you could theoretically use _identity as a unique identifier; but I gather they made that variable private for a reason!


import multiprocessing
def twos_multiple(x):
    proc = multiprocessing.Process()
    curr_proc = multiprocessing.current_process()
    print('current process:',, curr_proc._identity)
    print('created process:',, proc._identity)
    return x * 2
pro = multiprocessing.Pool()
print(, range(10)))


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :