0

I would like to have the python generated subprocesses (multiprocessing) report their python-assigned dict "name" into the system level log such that I can use existing process tracking utilities, without having to map the PID individually to the name provided in my application.

Initially, when spawning a multiprocessing.Process(...name="procName") I can see that the ps utility does not inherit the name provided internally in Python's Process properties, but instead as a duplicate of the parent thread. (This feature limitation is reasonable from both software and OS perspective) OS process name not being handed python's process name parameter (OS process name not being handed python's process name parameter)

I have done some work arounds to modify the value of the Process using a more direct memory modification via c library through Cython.

This appears to work in some manner, but appears to only appear in ps correctly when the process is defunct. OS process spawns with parent name assignment, followed by my approach forcing the process name update with lower-level communication clib, as shown by the process name updating (same PID, not shown in snippet though (OS process spawns with parent name assignment, followed by my approach forcing the process name update with lower-level communication clib, as shown by the process name updating (same PID, not shown in snippet though)

Can I get this to update on currently-running processes (long running)? In the mean-time I have dumped the processes into a log on-demand...but now also have to cross-reference the PID's to get the Python process names.

EDIT: here's a dirty quick test script just to demo the approach of modifying on clib. the result "works" as shown above - but only after the child process is complete.

from multiprocessing import current_process, Process, active_children
import time

def set_proc_name(newname):
    from ctypes import cdll, byref, create_string_buffer
    libc = cdll.LoadLibrary('libc.so.6')
    buff = create_string_buffer(len(newname)+1)
    buff.value = newname
    s = libc.prctl(15, byref(buff), 0, 0, 0)
    return s

def get_proc_name():
    from ctypes import cdll, byref, create_string_buffer
    libc = cdll.LoadLibrary('libc.so.6')
    buff = create_string_buffer(128)
    # 16 == PR_GET_NAME from <linux/prctl.h>
    libc.prctl(16, byref(buff), 0, 0, 0)
    return buff.value

EDIT2: setproctitle package clearly works here as suggested by @AKX - I will dive into this cython library and find out what approach they are using and update... enter image description here

Elysiumplain
  • 711
  • 8
  • 21
  • 3
    You can try [`setproctitle`](https://pypi.org/project/setproctitle/), e.g. in the `initializer` function of your subprocess. – AKX May 17 '23 at 18:54
  • get PID via Process,pid() and change it with os.system(bashCommand) https://stackoverflow.com/questions/67161935/change-process-title-name-in-bash-script – pippo1980 May 17 '23 at 20:07
  • simply writing isn't a working solution - The information that you read from the `proc` filesystem is not stored on any media (not even in RAM), so there is nothing to update. The data that you see when you read from the `proc` filesystem is generated on-the-fly in a manner NOT like `named-pipes`, but more like a `file-as-an-interface` to the OS `kernel`. The answer https://unix.stackexchange.com/questions/121702/what-happens-when-i-run-the-command-cat-proc-cpuinfo has some very good exploratory information. Different OS flavors handle this differently it appears. – Elysiumplain May 19 '23 at 19:55

0 Answers0