classification
Title: There is no proper way to know if a process is the main one
Type: enhancement Stage: patch review
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Delgan, ZackerySpytz, davin, pablogsal, pitrou
Priority: normal Keywords: patch

Created on 2019-10-18 20:48 by Delgan, last changed 2020-12-02 23:59 by ZackerySpytz.

Pull Requests
URL Status Linked Edit
PR 23621 open ZackerySpytz, 2020-12-02 23:59
Messages (1)
msg354918 - (view) Author: Delgan (Delgan) * Date: 2019-10-18 20:48
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?
History
Date User Action Args
2020-12-02 23:59:25ZackerySpytzsetkeywords: + patch
nosy: + ZackerySpytz

pull_requests: + pull_request22489
stage: patch review
2019-10-19 20:14:43pablogsalsetnosy: + pablogsal
2019-10-18 23:09:05ned.deilysetnosy: + pitrou, davin
2019-10-18 20:48:15Delgancreate