[New-bugs-announce] [issue38520] There is no proper way to know if a process is the main one

Adrien report at bugs.python.org
Fri Oct 18 16:48:15 EDT 2019


New submission from Adrien <delgan.py at gmail.com>:

Hi.

I noticed that there exists the function "threading.main_thread()", but there is no equivalent for the "multiprocessing" module.

Is there a reason for this absence?

These StackOverflow questions are related to this problem:
- https://stackoverflow.com/questions/42283265/how-to-determine-if-running-current-process-is-parent
- https://stackoverflow.com/questions/30790660/python-multiprocessing-name-of-the-main-process
- https://stackoverflow.com/questions/18216050/is-there-a-way-to-check-if-a-module-is-being-loaded-by-multiprocessing-standard

Current proposed solutions have flaws.
It's suggested to check if "current_process().name == 'MainProcess'" but anyone can re-write the name of a process with "current_process().name = 'foobar'" so this does not seem to be a reliable solution.
Another alternative is to check for the type of the process, which is different for main and child, respectively "mulitprocessing.process._MainProcess" and "multiprocessing.process.Process". This is obviously hacky as it relies on implementation detail.

What are you thoughts on adding a new "multiprocessing.main_process()" function?

----------
components: Library (Lib)
messages: 354918
nosy: Delgan
priority: normal
severity: normal
status: open
title: There is no proper way to know if a process is the main one
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38520>
_______________________________________


More information about the New-bugs-announce mailing list