Python Glossary Русский

all Python 3osfunction

os.getpid()

process

Returns the PID (identifier) of the current process. Useful for logging and for giving temporary files unique names.

Syntax

os.getpid() -> int

After fork the child has its own PID, so a value saved before fork is wrong in the child — call getpid again. A PID is unique only while the process lives: after it exits the number may go to another process. For unique temporary file names the tempfile module is more reliable.

Examples

import os
print(os.getpid())  # → ? PID текущего процесса, например 12345
print(type(os.getpid()))  # → <class 'int'>
print(os.getpid() > 0)  # → True
tmp_name = f'/tmp/app_{os.getpid()}.tmp'  # уникальное имя на процесс

See also

Open in the full glossarydocs.python.orgReport an inaccuracy