How to know what is the PID of a Python script currently running?

2

I have a certain Python script that I want to know what the current PID is through the script itself.

How can I do this?

Through this too, can I determine that it runs only once?

    
asked by anonymous 22.03.2017 / 15:20

1 answer

4

Before answering, talk a little about PID .

  

Process identifier, PID or even process ID is a unique number that is assigned by the operating system when a process is executed. This number is used to reference a process you are running.

source: wikipedia

In short, every process running on the operating system will have its own PID .

There are a few ways to do this, but the simplest thing is to just import the os and use the getpid() to get the PID of the current process.

Example:

import os

pid = os.getpid()

You can also get the PID based on the process name, and other forms.

See other forms in this question.

    
22.03.2017 / 15:31