How to Get the Process PID in Python - Super Fast Python

You can get the process pid via the multiprocessing.Process.pid attribute or via the os.getpid() and os.getppid() functions.

In this tutorial you will discover how to get the process pid in Python.

Let’s get started.

A process is a running instance of a computer program.

Every Python program is executed in a Process, which is a new instance of the Python interpreter. This process has the name MainProcess and has one thread used to execute the program instructions called the MainThread. Both processes and threads are created and managed by the underlying operating system.

Sometimes we may need to create new child processes in our program in order to execute code concurrently.

Python provides the ability to create and manage new processes via the multiprocessing.Process class.

You can learn more about multiprocessing in the tutorial:

In multiprocessing, we may need to get the process identifier or PID of a process.

For example:

  • We may need the pid of the current process.
  • We may need the pid of a child process.
  • We may need the pid of the parent process.

How can we get the pid of a process in Python?

What is a PID

PID is an acronym for Process ID or Process identifier.

A process identifier is a unique number assigned to a process by the underlying operating system.

Each time a process is started, it is assigned a unique positive integer identifier and the identifier may be different each time the process is started.

The pid uniquely identifies one process among all active processes running on the system, managed by the operating system.

As such, the pid can be used to interact with a process, e.g. to send a signal to the process to interrupt or kill a process.

How to Get a Process PID

We can get the pid from the multiprocessing.Process instance or via os module functions such as os.getpid() and os.getppid().

Let’s take a closer look at each approach in turn.

PID via Process Instance

We can get the pid of a process via its multiprocessing.Process instance.

When we create a child process, we may hang on to the process instance.

Alternatively, we may get the process instance for the parent process via the multiprocessing.parent_process() function or for child process via the multiprocessing.active_children() function.

We may also get the process instance for the current process via the multiprocessing.current_process() function.

For example:

...

# get the process instance

process = multiprocessing.current_process()

Once we have the process instance, we get the pid via the multiprocessing.Process.pid attribute.

For example:

...

# get the pid

pid = process.pid

PID via Module Functions

We can get the pid for the current process via the os.getpid() function.

For example:

...

# get the pid for the current process

pid = os.getpid()

We may also get the pid for the parent process via the os.getppid() function.

For example:

...

# get the pid for the parent process

pid = os.getppid()

Now that we know how to get the pid, let’s look at some worked examples.


Free Python Multiprocessing Course

Download your FREE multiprocessing PDF cheat sheet and get BONUS access to my free 7-day crash course on the multiprocessing API.

Discover how to use the Python multiprocessing module including how to create and start child processes and how to use a mutex locks and semaphores.

Learn more
 


Get Current Process PID

We can get the pid for the current process via the os.getpid() function.

...

# get the pid

pid = getpid()

The complete example is listed below.

# SuperFastPython.com

# example of getting the pid of the current process

from os import getpid

# get the pid

pid = getpid()

# report the pid

print(pid)

Running the example gets and reports the pid for the current process.

Note, the pid will differ each time the example is run.

Get Current Process PID via the Process Instance

We can get the pid for the current process via the Process instance.

This can be achieved by first getting the multiprocessing.Process instance for the current process.

...

# get the current process instance

process = current_process()

We can then access the pid attribute.

...

# get the pid

pid = process.pid

The complete example is listed below.

# SuperFastPython.com

# example of getting the pid of the current process process instance

from multiprocessing import current_process

# get the current process instance

process = current_process()

# get the pid

pid = process.pid

# report the pid

print(pid)

Running the example gets the process instance for the current process, then reports the pid.

Note, the pid will differ each time the example is run.


Python Multiprocessing Jump-Start

Loving The Tutorials?

Why not take the next step? Get the book.

Learn more
 


Get Parent Process PID

We can get the pid of the parent process via the os.getppid() function.

In this example we can run a custom function in a new child process. The child process will report the pid of the parent process.

First, we need to define a function to run in the child process.

The function will get the pid of the parent process and report it directly.

The task() function below implements this.

# function executed in the child process

def task():

    # get the parent process pid

    pid = getppid()

    # report the pid

    print(pid)

The main process will configure a new multiprocessing.Process instance to execute our custom function, start the process, then wait for the child process to terminate.

...

# configure the child process

child = Process(target=task)

# start the child process

child.start()

# wait for the child process to finish

child.join()

Tying this together, the complete example is listed below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

# SuperFastPython.com

# example of getting the pid of the parent process

from multiprocessing import Process

from os import getppid

# function executed in the child process

def task():

    # get the parent process pid

    pid = getppid()

    # report the pid

    print(pid)

# protect the entry point

if __name__ == '__main__':

    # configure the child process

    child = Process(target=task)

    # start the child process

    child.start()

    # wait for the child process to finish

    child.join()

Running the example configures and starts the child process.

The child process runs, gets the pid of the parent process and reports the value.

Note, the pid will differ each time the example is run.

Get Parent Process PID via the Process Instance

We can get the pid of the parent process via the multiprocessing.Process instance.

In this example we can run a custom function in a new child process. The child process will get the process instance for the parent process, then get the pid from the process instance and report its value.

First, we need to define a function to run in the child process.

The function will first get the multiprocessing.Process instance for the parent process via the multiprocessing.parent_process() function. It will then get the pid from the process instance and report the value.

The task() function below implements this.

# function executed in the child process

def task():

    # get the parent process

    parent = parent_process()

    # get the parent process pid

    pid = parent.pid

    # report the pid

    print(pid)

The main process will configure a new multiprocessing.Process instance to execute our custom function, start the process, then wait for the child process to terminate.

...

# configure the child process

child = Process(target=task)

# start the child process

child.start()

# wait for the child process to finish

child.join()

Tying this together, the complete example is listed below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

# SuperFastPython.com

# example of getting the pid of the parent process via process instance

from multiprocessing import Process

from multiprocessing import parent_process

# function executed in the child process

def task():

    # get the parent process

    parent = parent_process()

    # get the parent process pid

    pid = parent.pid

    # report the pid

    print(pid)

# protect the entry point

if __name__ == '__main__':

    # configure the child process

    child = Process(target=task)

    # start the child process

    child.start()

    # wait for the child process to finish

    child.join()

Running the example configures and starts the child process.

The child process runs, gets the parent process instance, then gets the pid of the parent process and reports the value.

Note, the pid will differ each time the example is run.

Get Child Process PID

We can get the pid of an active child process via the multiprocessing.Process instance.

In this example we will start a child process that executes a custom function. The parent process will then report the pid of the child process directly via the process instance.

First, we need to define a function to execute in the child process.

The function will block for a few seconds, giving the main process enough time to report the process pid.

The task() function below implements this.

# function executed in the child process

def task():

    sleep(2)

Next, the main process will configure and start a new child process to execute the custom function.

...

# configure the child process

child = Process(target=task)

# start the child process

child.start()

The main process will then get the pid of the child process via the multiprocessing.Process instance directly, then report the value.

...

# get the pid of the child process

pid = child.pid

# report the pid

print(pid)

Finally, the main process will wait for the child process to terminate.

...

# wait for the child process to finish

child.join()

Tying this together, the complete example is listed below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

# SuperFastPython.com

# example of getting the pid of a child process

from time import sleep

from multiprocessing import Process

# function executed in the child process

def task():

    sleep(2)

# protect the entry point

if __name__ == '__main__':

    # configure the child process

    child = Process(target=task)

    # start the child process

    child.start()

    # get the pid of the child process

    pid = child.pid

    # report the pid

    print(pid)

    # wait for the child process to finish

    child.join()

Running the example configures and starts the child process.

The child process runs and blocks for a few seconds.

The main process then gets the pid of the child process and reports its value. It then blocks until the child process finishes.

Note, the pid will differ each time the example is run.

Further Reading

This section provides additional resources that you may find helpful.

Python Multiprocessing Books

I would also recommend specific chapters in the books:

Guides

APIs

References

    Takeaways

    You now know how to get the process pid in Python.

    Do you have any questions?
    Ask your questions in the comments below and I will do my best to answer.

    Photo by Gene Gallin on Unsplash