A Python module to customize the process title
npx @tessl/cli install tessl/pypi-setproctitle@1.3.0A cross-platform Python module that allows processes to customize their title as displayed by system monitoring tools like ps, top, and Activity Monitor. The module wraps around PostgreSQL's multi-platform implementation and includes both a C extension for optimal performance and pure Python fallback functions.
pip install setproctitleimport setproctitleFor importing specific functions:
from setproctitle import setproctitle, getproctitle, setthreadtitle, getthreadtitleimport setproctitle
# Set the process title
setproctitle.setproctitle("my-worker-process")
# Get the current process title
current_title = setproctitle.getproctitle()
print(f"Current process title: {current_title}")
# Set thread title (on supported platforms)
setproctitle.setthreadtitle("worker-thread-1")
# Get current thread title
thread_title = setproctitle.getthreadtitle()
print(f"Current thread title: {thread_title}")The module provides a dual-implementation architecture:
_setproctitle): High-performance native implementation for optimal performanceFunctions for setting and retrieving the current process title as displayed by system monitoring tools.
def setproctitle(title: str) -> None:
"""
Set title as the title for the current process.
Parameters:
- title (str): The new process title to set
Returns:
None
Notes:
The process title is visible in /proc/PID/cmdline, /proc/PID/status,
/proc/PID/comm files and used by tools like ps and top.
"""
def getproctitle() -> str:
"""
Return the current process title.
Parameters:
None
Returns:
str: Current process title, or command line arguments joined by spaces
if C extension unavailable
"""Functions for setting and retrieving the current thread title on supported platforms.
def setthreadtitle(title: str) -> None:
"""
Set title as the title for the current thread.
Parameters:
- title (str): The new thread title to set
Returns:
None
Notes:
Thread title is exposed as /proc/PID/task/TID/comm on supported platforms
and used by tools like htop.
"""
def getthreadtitle() -> str:
"""
Get the current thread title.
Parameters:
None
Returns:
str: Current thread title, or empty string if C extension unavailable
"""The module behavior can be customized using environment variables:
SPT_NOENV: Set to any non-empty value to avoid clobbering /proc/PID/environ. This limits the maximum title length to the command line length but preserves the environ file.
SPT_DEBUG: Set to any non-empty value to print debug information on stderr. Most useful information is printed during module import.
sys/prctl.h for process title manipulationsetproctitle() system callHAVE_SETPROCTITLE and HAVE_PS_STRING definitionsdarwin_set_process_name.c for platform-specific implementationgetproctitle() call to avoid fork() issues__darwin__ symbolThe module provides graceful degradation:
SPT_DEBUG environment variableimport os
import setproctitle
from multiprocessing import Process
def worker_process(worker_id):
# Set descriptive process title for this worker
setproctitle.setproctitle(f"myapp-worker-{worker_id}")
# Do work...
print(f"Worker {worker_id} running with title: {setproctitle.getproctitle()}")
if __name__ == "__main__":
# Set main process title
setproctitle.setproctitle("myapp-master")
# Start worker processes
processes = []
for i in range(3):
p = Process(target=worker_process, args=(i,))
p.start()
processes.append(p)
# Wait for all workers
for p in processes:
p.join()import threading
import setproctitle
def worker_thread(thread_name):
# Set thread title for monitoring
setproctitle.setthreadtitle(f"worker-{thread_name}")
# Do thread work...
print(f"Thread {thread_name} title: {setproctitle.getthreadtitle()}")
# Create and start threads
threads = []
for i in range(3):
t = threading.Thread(target=worker_thread, args=(f"thread-{i}",))
t.start()
threads.append(t)
# Wait for completion
for t in threads:
t.join()