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)
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)
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...