How can I serve a (never ending) log file with Tornado?
-
Hi, In a certain application i want to give the user the ability to watch the log file as it gets new line as if one would have been typing "tail -f logfile" on the server. so I got that example of `cat file` which when replacing `cat` with `tail -f` simply does not work. I guess the reason is that 'tail -f' is never closed, therefore callbacks are never triggered. I think what I need is actually composing my own object while I wonder what kind of an object must that one be, saying what interface must such an object implements https://gist.github.com/671069 below is the cat code I was mentioning above #!/usr/bin/env python import os import tornado.httpserver import tornado.ioloop import tornado.web class StreamHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): #self.set_header("Content-Type", "application/ogg+ogg") self.ioloop = tornado.ioloop.IOLoop.instance() self.pipe = os.popen('cat foo.log') self.ioloop.add_handler( self.pipe.fileno(), self.async_callback(self.on_response), self.ioloop.READ ) def on_response(self,fd,events): for line in self.pipe: self.write( line ) self.ioloop.remove_handler(fd) self.finish() application = tornado.web.Application([ (r"/", StreamHandler), ]) if __name__ == "__main__": http_server = tornado.httpserver.HTTPServer(application) http_server.listen(8888) tornado.ioloop.IOLoop.instance().start()
-
Answer:
You probably could just use Python's internals to read the file you want to serve to the client. Something along the lines: class TailHandler(tornado.web.RequestHandler): def __init__(self, *a, **kw): self.file = open('/path/to/myfile.txt') self.pos = self.file.tell() @tornado.asynchronous def get(self): def _read_file(): # Read some amout of bytes here. You can't read until newline as it # would block line = self.file.read(4096) if not line: self.file.seek(self.pos) else: self.write(line) self.flush() IOLoop.instance().add_timeout(time.time() + 1, _read_file) _read_file() Now I can't guarantee that this works, but you could do something along these lines. Edit: There should also be error checking, proper closing the file etc.
Jyrki Pulliainen at Quora Visit the source
Related Q & A:
- How can I send a .mht file as blat attachment?Best solution by Stack Overflow
- How can I do a resource in PDF file?Best solution by Stack Overflow
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- How can I burn a .rmvb file onto a DVD for viewing on a regular DVD player?Best solution by Yahoo! Answers
- How can I send a file to a friend on Hotmail?Best solution by askdavetaylor.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.