如何将Web服务器添加到现有的长时间运行的Python程序?

huangapple go评论68阅读模式
英文:

How to add a web server to an existing long-running Python program?

问题

我有一个长时间运行的Python程序,定期执行工作。我需要它还能够托管一个Web服务器,以便提供指标和其他管理功能。我想使用Flask作为Web框架,虽然这不是硬性要求 - 我只需要路由一些URL。有适合在后台线程上托管的Web服务器吗?

我想要的解决方案类似于Golang的net/http Web服务器,其中我可以在其自己的goroutine上托管并启动/停止它。

我已经使用Flask内置的开发服务器使其工作,但其文档建议不要在生产中使用。同样,适用于Python的http.server

编辑:我还尝试在后台线程上使用GunicornBaseApplication,但在幕后,它监听操作系统信号,这在除了主Python解释器线程以外是不允许的。对于twisted也是如此。

英文:

I have a long-running Python program that is periodically performing work. I have a requirement for it to also host a web server so that it can serve metrics and other admin capabilities. I'd like to use Flask as the web framework, although this isn't a deal-breaker - I only need to route a few URLs. Is there a suitable web server that I can host on a background thread?

I suppose the solution I'm looking for is akin to Golang's net/http web server wherein I can host and start/stop it on its own goroutine.

I have got this working using Flask's inbuilt development server but its documentation advises that this should not be used in production. Likewise for Python's http.server.

Edit: I have also tried using Gunicorn's BaseApplication on a background thread but, under-the-hood, it listens for OS signals which isn't allowed on anything but the main Python interpreter thread. Ditto for twisted.

答案1

得分: 1

Flask不适合生产环境,因为它不具备良好的扩展性...但是你可以使用像Bottle这样的框架。

英文:

Flask is not suitable for production because it doesn't scale well...
however you can use a framework like Bottle.

huangapple
  • 本文由 发表于 2023年5月13日 13:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定