【问题标题】:web.py hosted on Apache does not run code in side if __name__ == "__main__":如果 __name__ == "__main__",则托管在 Apache 上的 web.py 不会运行代码:
【发布时间】:2017-06-17 20:24:49
【问题描述】:

我的代码与我在question 中提到的代码相同。现在我在 Apache 上托管了相同的 web.py 应用程序。但是当我启动 Apache 时,if __name__ == "__main__": 中的代码不会被执行。

在 Apache 中托管时是否可以运行后台进程(检查代码的其他问题)?

为什么if __name__ == "__main__":里面的代码没有执行?

当 web.py 在没有 Apache 的情况下运行时效果很好。

【问题讨论】:

标签: python apache mod-wsgi wsgi web.py


【解决方案1】:

if __name__ == '__main__': 中的代码无法运行,因为 Apache 不是这样运行 python 代码的。

更有可能的是,您在 mod_wsgi 或 uwsgi 下运行您的 python,这是让 Apache 与 python 对话的一种方式。

保留if __name__ == '__main__': 的东西:这对于简单的测试很有用,但添加一个类似的块:

if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()
elif under_mod_wsgi or under_uwsgi:
    app = web.application(urls, globals())
    application = app.wsgifunc()  # !!rather than app.run()

您的 Process 内容应该仍然可以运行(参考您的其他问题)。

要检测under_mod_wsgi是否可以:

try:
    from mod_wsgi import version
    if version:
        pass
    under_mod_wsgi = True
except ImportError:
    under_mod_wsgi = False

try:
    import uwsgi
    under_uwsgi = True
except ImportError:
    under_uwsgi = False

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 2018-03-13
    • 2020-06-05
    • 2015-09-12
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多