python 部署 CherryPy(守护进程)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1463510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:19:46  来源:igfitidea点击:

Deploying CherryPy (daemon)

pythondeploymentcherrypy

提问by erikcw

I've followed the basic CherryPy tutorial (http://www.cherrypy.org/wiki/CherryPyTutorial). One thing not discussed is deployment.

我遵循了基本的 CherryPy 教程 ( http://www.cherrypy.org/wiki/CherryPyTutorial)。没有讨论的一件事是部署。

How can I launch a CherryPy app as a daemon and "forget about it"? What happens if the server reboots?

如何将 CherryPy 应用程序作为守护程序启动并“忘记它”?如果服务器重新启动会发生什么?

Is there a standard recipe? Maybe something that will create a service script (/etc/init.d/cherrypy...)

有标准配方吗?也许会创建服务脚本的东西(/etc/init.d/cherrypy...)

Thanks!

谢谢!

采纳答案by Benno

There is a Daemonizerplugin for CherryPy included by default which is useful for getting it to start but by far the easiest way for simple cases is to use the cherryd script:

CherryPy 默认包含一个Daemonizer插件,这对于启动它很有用,但到目前为止,对于简单情况,最简单的方法是使用cherryd 脚本:

> cherryd -h
Usage: cherryd [options]

Options:
  -h, --help            show this help message and exit
  -c CONFIG, --config=CONFIG
                        specify config file(s)
  -d                    run the server as a daemon
  -e ENVIRONMENT, --environment=ENVIRONMENT
                        apply the given config environment
  -f                    start a fastcgi server instead of the default HTTP
                        server
  -s                    start a scgi server instead of the default HTTP server
  -i IMPORTS, --import=IMPORTS
                        specify modules to import
  -p PIDFILE, --pidfile=PIDFILE
                        store the process id in the given file

As far as an init.d script goes I think there are examples that can be Googled.

就 init.d 脚本而言,我认为有些示例可以通过 Google 搜索。

And the cherrydis found in your:

并且cherryd在您的:

virtualenv/lib/python2.7/site-packages/cherrypy/cherryd

virtualenv/lib/python2.7/site-packages/cherrypy/cherryd

or in: https://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd

或在:https: //bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd

回答by lysdexia

Daemonizer can be pretty simple to use:

Daemonizer 使用起来非常简单:

# this works for cherrypy 3.1.2 on Ubuntu 10.04
from cherrypy.process.plugins import Daemonizer
# before mounting anything
Daemonizer(cherrypy.engine).subscribe()

cherrypy.tree.mount(MyDaemonApp, "/")
cherrypy.engine.start()
cherrypy.engine.block()

There is a decent HOWTO for SysV style here.

这里有一个 SysV 风格的体面 HOWTO。

To summarize:

总结一下:

  1. Create a file named for your application in /etc/init.dthat calls /bin/sh

    sudo vim /etc/init.d/MyDaemonApp

    #!/bin/sh  
    echo "Invoking MyDaemonApp";  
    /path/to/MyDaemonApp  
    echo "Started MyDaemonApp. Tremble, Ye Mighty."  
    
  2. Make it executable

    sudo chmod +x /etc/init.d/MyDaemonApp

  3. Run update-rc.dto create our proper links in the proper runtime dir.

    sudo update-rc.d MyDaemonApp defaults 80

  4. sudo /etc/init.d/MyDaemonApp

  1. /etc/init.d该调用中创建一个以您的应用程序命名的文件/bin/sh

    sudo vim /etc/init.d/MyDaemonApp

    #!/bin/sh  
    echo "Invoking MyDaemonApp";  
    /path/to/MyDaemonApp  
    echo "Started MyDaemonApp. Tremble, Ye Mighty."  
    
  2. 使其可执行

    sudo chmod +x /etc/init.d/MyDaemonApp

  3. 运行update-rc.d以在适当的运行时目录中创建适当的链接。

    sudo update-rc.d MyDaemonApp defaults 80

  4. sudo /etc/init.d/MyDaemonApp

回答by saaj

I wrote a tutorial/project skeleton, cherrypy-webapp-skeleton, which goal was to fill the gaps for deploying a real-world CherryPy application on Debian* for a web-developer. It features extended cherrydfor daemon privilege drop. There's also a number of important script and config files for init.d, nginx, monit, logrotate. The tutorial part describes how to put things together and eventually forget about it. The skeleton part proposes a way of possible arrangement of CherryPy webapp project assets.

我编写了一个教程/项目框架cherrypy-webapp-skeleton,其目标是填补在Debian* 上为Web 开发人员部署真实CherryPy 应用程序的空白。它的功能扩展cherryd为守护进程权限下降。还有许多重要的脚本和配置文件,用于init.d, nginx, monit, logrotate。教程部分描述了如何将事情放在一起并最终忘记它。骨架部分提出了一种可能的 CherryPy webapp 项目资产排列方式。



* It was written for Squeeze but practically it should be same for Wheezy.

* 它是为 Squeeze 编写的,但实际上它应该与 Wheezy 相同。

回答by chiefenne

Info on Daemonizer options

有关 Daemonizer 选项的信息

When using Daemonizer, the docsdon't state the options, e.g. how to redirect stdoutor stderr. From the source of the Daemonizerclass you can find the options. As a reference take this example from my project:

使用 Daemonizer 时,文档没有说明选项,例如如何重定向stdoutstderr。从Daemonizer类的源代码中,您可以找到选项。作为参考,从我的项目中获取此示例:

# run server as a daemon
d = Daemonizer(cherrypy.engine,
               stdout='/home/pi/Gate/log/gate_access.log',
               stderr='/home/pi/Gate/log/gate_error.log')
d.subscribe()