【问题标题】:Deploying CherryPy (daemon)部署 CherryPy(守护进程)
【发布时间】:2010-11-30 14:45:09
【问题描述】:

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

如何将 CherryPy 应用程序作为守护程序启动并“忘记它”?如果服务器重启会怎样?

有标准食谱吗?也许会创建一个服务脚本(/etc/init.d/cherrypy...)

谢谢!

【问题讨论】:

    标签: python deployment cherrypy


    【解决方案1】:

    默认包含一个用于 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
    

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

    cherryd 位于您的:

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

    或输入:https://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd

    【讨论】:

      【解决方案2】:

      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.

      总结一下:

      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

      【讨论】:

      • 我不知道spizouzou 是什么,但这个变量是完全没有必要的。简单地说:Daemonizer(cherrypy.engine).subscribe() 并且插件将持续存在,因为 engine 现在保持对它的引用。
      • @brandon craig rhodes 是正确的。无需创建变量。
      • tools.cherrypy.org/wiki/WindowsService 正如其他地方所提到的,创建 Windows 服务的工作量大致相同。注意页面底部的日志位!它们很重要。
      • 更改以反映@BrandonRhodes 的批评。
      【解决方案3】:

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


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

      【讨论】:

      • 本教程中的代码真的很有帮助,谢谢
      【解决方案4】:

      有关守护程序选项的信息

      当使用 Daemonizer 时,docs 不会说明选项,例如如何重定向 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-01
        • 1970-01-01
        • 2023-03-02
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多