py-setproctitle Documentation

repository·master·Indexed 20 days ago

https://github.com/dvarrazzo/py-setproctitle

A Python C extension that allows processes and threads to customize their titles as displayed in system monitoring tools like ps, top, and htop. Supports GNU/Linux, BSD, MacOS X, and Windows. Provides APIs for setting and getting process and thread titles, and configurable behavior via SPT_NOENV and SPT_DEBUG environment variables.

Tokens
544
Snippets
2
Records
4
Agent score
19%

What's inside py-setproctitle

  1. Install setproctitle via pip

    master

    To install setproctitle, use pip. Because this is a C extension, you must have a C compiler and Python development headers (e.g., python-dev or python3-dev on Linux) installed on your system before running the installation command.

    pip install setproctitle
  2. Configure setproctitle via environment variables

    master

    You can customize the behavior of the setproctitle module using the following environment variables:

    VariableDescription
    SPT_NOENVIf set to any non-empty value, the module avoids clobbering /proc/PID/environ. Note that this limits the maximum title length to the length of the command line.
    SPT_DEBUGIf set to any non-empty value, the module prints debug information to stderr. Most useful information is printed during module import.
  3. Use setproctitle to manage process and thread titles

    master

    The setproctitle module allows you to change how your process and its threads appear in system monitoring tools like ps, top, or htop.

    Important: Import and use the module as early as possible in your program's lifecycle. Writing environment variables later in your code may interfere with the module's initialization.

    Process Title API

    • setproctitle(title): Sets the title for the current process.
    • getproctitle(): Returns the current process title.

    Thread Title API

    • setthreadtitle(title): Sets the title for the current thread.
    • getthreadtitle(): Returns the current thread title.
    import setproctitle
    
    # Set and get process title
    setproctitle.setproctitle("my_custom_process_name")
    print(setproctitle.getproctitle())
    
    # Set and get thread title
    setproctitle.setthreadtitle("my_worker_thread")
    print(setproctitle.getthreadtitle())