使用 WSGI 在守护进程模式下运行 pdb

发布于 2024-12-02 07:59:36 字数 247 浏览 2 评论 0原文

我正在 Apache 2.2 上使用 mod wsgi 运行 Python 脚本。

是否可以在 wsgi 中使用守护进程模式在 python 脚本中运行 pdb.set_trace() ?

编辑 我想使用守护程序模式而不是嵌入式模式的原因是能够重新加载代码,而不必每次都重新启动 Apache 服务器(嵌入式模式需要)。我希望能够使用代码重新加载,而无需每次重新启动 Apache,并且仍然能够使用 pdb...

I am running a Python script on Apache 2.2 with mod wsgi.

Is it possible to run pdb.set_trace() in a python script using daemon mode in wsgi?

Edit
The reason I want to use daemon mode instead of embedded mode is to have the capability to reload code without having to restart the Apache server every time (which embedded mode requires). I would like to be able to use code reloading without restarting Apache everytime and still be able to use pdb...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-12-09 07:59:36

我也有同样的需求,希望能够使用功能极其强大的 pdb,只要我想调试 Python 服务器代码的某些部分,就可以删除 pdb.set_trace()

是的,Apache 在您无法控制的地方生成 WSGI 应用程序 [1]。但我发现一个很好的折衷方案是

  1. 维护您的 Apache WSGIScriptAlias

  2. 并且还可以选择启动您的 Python服务器也在终端中(在本例中进行本地测试,不再通过 Apache 进行测试)

如果有人使用 WSGIScriptAlias 有点像这样......
指向名为 webserver.py 的 Python WSGI 脚本

<VirtualHost *:443>

    ServerName myawesomeserver
    DocumentRoot /opt/local/apache2/htdocs

    <Directory /opt/local/apache2/htdocs>
        [...]
    </Directory>

    WSGIScriptAlias /myapp /opt/local/apache2/my_wsgi_scripts/webserver.py/

    <Directory /opt/local/apache2/my_wsgi_scripts/>
        [...]
    </Directory>

    [...]
    SSLEngine on
    [...]
</VirtualHost>                                  

,因此您的 webserver.py 可以有一个简单的开关来在 Apache 使用和开始手动调试。

在您的配置文件中保留一个标志,例如在某些 settings.py 中:

WEBPY_WSGI_IS_ON = True

webserver.py

import web
import settings

urls = (
    '/', 'excellentWebClass',
    '/store', 'evenClassier',)

if settings.WEBPY_WSGI_IS_ON is True:
    # MODE #1: Non-interactive web.py ; using WSGI
    #   So whenever true, the Web.py application here will talk wsgi.
    application = web.application(urls, globals()).wsgifunc()

class excellentWebClass:
    def GET(self, name):

        # Drop a pdb  wherever you want only if running manually from terminal.
        pdb.set_trace()

        try:
            f = open (name)
            return f.read()
        except IOError:
            print 'Error: No such file %s' % name

if __name__ == "__main__":

    # MODE #2: Interactive web.py , for debugging.
    #   Here you call it directly.  
    app = web.application(urls, globals())
    app.run()

因此,当您想以交互方式测试您的网络服务器时,只需运行它从终端,

$ python webserver.py 8080
starting web...
http://0.0.0.0:8080/

[1] 脚注:有一些非常复杂的方法可以让 Apache 子进程处于您的控制之下,但我认为如果您只想调试 Python 服务器代码,上面的方法要简单得多。如果确实有简单的方法,那么我也很想了解这些。

I had the same need to be able to use the amazingly powerful pdb, dropping a pdb.set_trace() wherever I wanted to debug some part of the Python server code.

Yes, Apache spawns the WSGI application in a place where it is out of your control [1]. But I found a good compromise is to

  1. maintain your Apache WSGIScriptAlias

  2. and also give yourself the option of starting your Python server in a terminal as well (testing locally and not through Apache anymore in this case)

So if one uses WSGIScriptAlias somewhat like this...
pointing to your python WSGI script called webserver.py

<VirtualHost *:443>

    ServerName myawesomeserver
    DocumentRoot /opt/local/apache2/htdocs

    <Directory /opt/local/apache2/htdocs>
        [...]
    </Directory>

    WSGIScriptAlias /myapp /opt/local/apache2/my_wsgi_scripts/webserver.py/

    <Directory /opt/local/apache2/my_wsgi_scripts/>
        [...]
    </Directory>

    [...]
    SSLEngine on
    [...]
</VirtualHost>                                  

And so your webserver.py can have a simple switch to go between being used by Apache and getting started up for debugging manually.

Keep a flag in your config file such as, in some settings.py:

WEBPY_WSGI_IS_ON = True

And webserver.py :

import web
import settings

urls = (
    '/', 'excellentWebClass',
    '/store', 'evenClassier',)

if settings.WEBPY_WSGI_IS_ON is True:
    # MODE #1: Non-interactive web.py ; using WSGI
    #   So whenever true, the Web.py application here will talk wsgi.
    application = web.application(urls, globals()).wsgifunc()

class excellentWebClass:
    def GET(self, name):

        # Drop a pdb  wherever you want only if running manually from terminal.
        pdb.set_trace()

        try:
            f = open (name)
            return f.read()
        except IOError:
            print 'Error: No such file %s' % name

if __name__ == "__main__":

    # MODE #2: Interactive web.py , for debugging.
    #   Here you call it directly.  
    app = web.application(urls, globals())
    app.run()

So when you want to test out your webserver interactively, you just run it from a terminal,

$ python webserver.py 8080
starting web...
http://0.0.0.0:8080/

[1] Footnote: There are some really complex ways of getting Apache child processes under your control, but I think the above is much simpler if you just want to debug your Python server code. And if there are actually easy ways, then I would love to learn about those too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文