Search code examples
pythonpython-3.xstreamvideo-streamingtornado

Tornado Python - Stream a video


I've been building a website for some time, and I'm still stuck with that thing:

I store some small videos (~400MB approx. at best) for my website inside of a dbm database, and I'd like to stream them on my website.

I'm building the request handlers by hand using the Tornado python framework, and I was wondering how to build my handler. I never found how media stream worked and didn't find a lot of topics on the web.

So the complete result I'd want to achieve is having a web player on my website, where I can request specific videos, then playing them without having to load the entire file in memory/send it in 1 request.


Solution

  • These two links:

    • One for Tornado only: this appears to use special annotations.
    • One for Flask: albeit a motion JPEG example, it shows how you
      can return a function that performs a "while" loop as a response.

    Appear to be the answers you are looking for. And guess what? So am I!

    Note that both use the "yield" keyword in python. It is unclear to me if the "coroutine" and "asynchronous" decorators are necessary in the Flask example (in other words, it is unclear if the example given in the link is complete...although, he literally wrote the book about it, so I suspect it is).

    Beware: tests show that tornado.web holds on to the ENTIRE file during download, even if you stream it (i.e. read, write, flush, read...). The reason for this is unclear, and I have yet to find a way around it.