0

I am developing a project using cherrypy, on debian. At my work, the administrators want to see the name of the project instead of "python" displayed when using commands like ps -e. However, when cherrypy auto-reloads when modifying one source file, it automatically changes the process name.

For example, if I take the most basic cherrypy tutorial and save it under NameToSee.py:

#!/usr/bin/python
import cherrypy


class HelloWorld(object):
    @cherrypy.expose
    def index(self):
        return "Hello world!"

if __name__ == '__main__':
   cherrypy.quickstart(HelloWorld())

By adding the shebang at the start, when I launch it $ ./NameToSee.py &, I get a process (say 31051) whose name is "NameToSee.py":

$ head /proc/31051/status
Name:   NameToSee.py
State:  S (sleeping)

However, whenever I change the source code files (for example, by adding an empty line), the process name changes:

$ head /proc/31051/status
Name:   python
State:  S (sleeping)

So, my question is: Can I get both cherrypy auto-reload and custom process name ? If not, can I remove the cherrypy auto-reload?

I am running on debian wheezy, with python 2.7.3 and cherrypy 3.2.2

DainDwarf
  • 1,651
  • 10
  • 19

1 Answers1

2

This sample covers both cases:

import cherrypy
from cherrypy.process.plugins import SimplePlugin

PROC_NAME = 'sample'


def set_proc_name(newname):
    """                                                                                                                                                                                                      
    Set the process name.                                                                                                                                                                                    
    Source: http://stackoverflow.com/a/923034/298371                                                                                                                                                         
    """
    from ctypes import cdll, byref, create_string_buffer
    libc = cdll.LoadLibrary('libc.so.6')
    buff = create_string_buffer(len(newname)+1)
    buff.value = newname
    libc.prctl(15, byref(buff), 0, 0, 0)


class NamedProcess(SimplePlugin):
    """                                                                                                                                                                                                      
    Set the name of the process everytime that the                                                                                                                                                           
    engine starts.                                                                                                                                                                                           
    """

    def start(self):
        self.bus.log("Setting the name as '{}'".format(PROC_NAME))
        set_proc_name(PROC_NAME)


class HelloWorld(object):

    @cherrypy.expose
    def index(self):
        return "Hello world!"


def run_without_autoreload():
    set_proc_name(PROC_NAME)
    cherrypy.quickstart(HelloWorld(), config={
        'global': {
            'engine.autoreload.on': False
        }
    })


def run_with_autoreload():
    # Work on any configuration but for the sake of the                                                                                                                                                      
    # question this works with the autoreload.                                                                                                                                                               
    NamedProcess(cherrypy.engine).subscribe()
    cherrypy.quickstart(HelloWorld())


if __name__ == '__main__':
    run_with_autoreload()
    # run_without_autoreload()

You can test it with:

cat /proc/`pgrep  sample`/status

(or maybe just use pgrep)

And as a final recommendation consider using the "production" environment (see the docs) which also included the disabling of the autoreload plugin.

cyraxjoe
  • 5,661
  • 3
  • 28
  • 42
  • That sounds great. Do you know if there's any way to get PR_SET_NAME value dynamically instead of having a magic number in the code? – DainDwarf Dec 16 '15 at 08:10