如何阻止cherrypy autoreload更改Debian上的进程名?

2024-04-16 09:21:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在debian上用cherrypy开发一个项目。在我的工作中,管理员希望看到项目的名称,而不是使用ps -e之类的命令时显示的“python”。但是,当cherrypy在修改一个源文件时自动重新加载时,它会自动更改进程名。在

例如,如果我学习最基本的cherrypy教程并将其保存在NameToSee.py下:

#!/usr/bin/python
import cherrypy


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

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

通过在开始处添加shebang,当我启动它时$ ./NameToSee.py &,我得到一个进程(比如31051),它的名称是“名称见.py“:

^{pr2}$

但是,每当我更改源代码文件时(例如,通过添加空行),进程名称将更改:

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

所以,我的问题是:我可以同时获得cherrypy自动重新加载和自定义进程名吗?如果没有,我可以移除cherrypy自动重新加载吗?

我在DebianWheezy上运行Python2.7.3和Cherrypy3.2.2


Tags: 项目py命令名称bin进程usr管理员
1条回答
网友
1楼 · 发布于 2024-04-16 09:21:24

此示例涵盖两种情况:

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

您可以用以下方法进行测试:

^{pr2}$

(或使用pgrep)

作为最后的建议,考虑使用“生产”环境(see the docs),这也包括禁用autoreload插件。在

相关问题 更多 >