Skip to content

[Linux, Darwin] psutil.Process names should be same as multiprocessing.Process names #2625

@ic

Description

@ic

Update

This remains an issue, but may get closed as non-fixable-without-changing Python itself. Adopting a different angle, I found an entry that seems typical on how to actually name multiprocessing.Process processes.

Summary

  • OS: Linux, Darwin
  • Type: core, perhaps

Description

psutil.Process names seem to differ from names decided in the multiprocessing standard lib. I wonder whether this is not something desired to have consistent outputs. Here is an example:

import multiprocessing
import time

import psutil

def work():
    time.sleep(1)

if __name__ == '__main__':
    p = psutil.Process()
    c = multiprocessing.Process(name="child", target=work)
    c.start()
    memo = [cp.name() for cp in p.children(recursive=True)]
    c.join()
    assert c.name in memo, f"{c.name} name should have been in the child process name list: {memo}"

It seems reasonable to expect that the name of the multiprocessing.Process process should be reported as-is in the children list of the parent process. Yet they differ: The multiprocessing.Process name is the same as the parent process. On the other hand, the official documentation on a process name in multiprocessing is not clear on this point. It may just be an "internal" name in the Python runtime, and not visible from outside. If so, I guess there is not much doable here.

This script has run on Linux (a standard Ubuntu 24) and Darwin (a Mac Intel on 15.3.2). The problem is the same on both systems, although the Mac output differs (different matter).

Related issues

I have not found a related topic by searching in GitHub with keywords like process, name, multiprocessing or name().

Extra

Here is the same test script, with logs to get more context:

import multiprocessing
import time

import psutil


def work():
    time.sleep(1)

if __name__ == '__main__':
    memo = []
    p = psutil.Process()
    c = multiprocessing.Process(name="child", target=work)
    print(f"parent={p.name()}, pid={p.pid}")
    print(f"children={p.children(recursive=True)} (expect 0)")
    c.start()
    memo = [cp.name() for cp in p.children(recursive=True)]
    print(f"children={p.children(recursive=True)} (expect 1)")
    c.join()
    print(f"children={p.children(recursive=True)} (expect 0)")

    assert c.name in memo, f"{c.name} name should have been in the child process name list: {memo}"

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions