开发者

Giving my Python application a web interface to monitor it, using Tornado

开发者 https://www.devze.com 2023-01-22 23:38 出处:网络
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 vari

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().s开发者_JS百科tart() 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?


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:

import threading
t = threading.Thread(target = tornado.ioloop.IOLoop.instance().start)
t.start()

Let me know if that works!


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.


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.


You could try using http://www.zeromq.org/ to as a means of communication between to the two processes / threads.

0

精彩评论

暂无评论...
验证码 换一张
取 消