使用 Tornado 为我的 Python 应用程序提供一个 Web 界面来监控它
我有一个 Python 应用程序,它是守护进程并在服务器上 24/7 运行。我希望能够提供一个非常简单的 Web 界面,以便我可以监视程序中一些变量的变化值。
我正在使用 Tornado,并且我正在使用简单的“Hello, world”运行,您可以在 Tornado 上找到它主页。但是,一旦调用 tornado.ioloop.IOLoop.instance().start()
,它就会进入循环并且不会返回。我现有的程序(本质上)也是一个无限循环,但我想将两者整合起来。
所以,我的问题是:如何构建我的程序,以便通过使用 Tornado 提供 Web 界面来监视无限循环内的变量?
I've got a Python application which is daemonized and running on a server 24/7. I'd like to be able to give an incredibly simple web interface so that I can monitor the changing values of a few variables within the program.
I'm using Tornado, and I'm up and running with the simple 'Hello, world' that you can find on the Tornado homepage. However, as soon as tornado.ioloop.IOLoop.instance().start()
is called, it enters into the loop and doesn't return. My existing program is (essentially) an infinite loop as well, but I want to integrate the two.
So, my question is: how can I construct my program so that I can monitor variables inside my infinite loop by using Tornado to provide a web interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是否可以使用
threading
包并在自己的线程内运行Tornado?编辑:
threading
模块文档位于http: //docs.python.org/library/threading.html 有更多详细信息,但我正在想象这样的事情:让我知道这是否有效!
Is it possible to use the
threading
package and run Tornado inside of its own thread?Edit:
The
threading
module documentation at http://docs.python.org/library/threading.html has more details, but I am imagining something like this:Let me know if that works!
我相信最好的(读:最简单的)方法是让您的守护程序应用程序将您想要监视的那些特定变量写入龙卷风应用程序可以访问的共享空间。这可以是文件、套接字、数据库或键值存储。我想到的一些想法是使用现有的数据库(如果有的话)sqlite,甚至memcached。然后,您只需让龙卷风应用程序从您存储这些值的位置读取这些值即可。
您是正确的,一旦您运行tornado.ioloop.IOLoop.instance().start(),龙卷风的控制流就永远不会从该循环返回。从那时起,您的应用程序的控制将保留在您定义的 Application 和 RequestHandler 内。
I believe that the best (read: easiest) approach would be to have your daemon app write those particular variables you want to monitor out to a shared spaced that your tornado app can access. This could be a file, a socket, a database, or key-value store. Some ideas ideas that come to mind is to use your existing database (if there is one,) sqlite, or even memcached. Then, you would simply have your tornado application read those values from wherever you stored them.
You are correct in that once you run
tornado.ioloop.IOLoop.instance().start()
tornado's control flow never returns from that loop. From that point forward, your application's control will stay within the Application and RequestHandlers that you defined.另一个不太优雅的解决方案是利用 yaml 定期从主应用程序序列化对象,并让 Web 应用程序读取这些对象。您甚至可以将对象转储到 yaml 中,这样您就可以看到它们的不同状态。
Another less elegant solution would be to utilize yaml to serialize the objects periodically from your main app, and have the web app read those in. You can even dump objects into yaml, so you could see the different states of those.
您可以尝试使用 http://www.zeromq.org/ 作为两者之间的通信方式进程/线程。
You could try using http://www.zeromq.org/ to as a means of communication between to the two processes / threads.