Implementing Streaming With Flask
Flask provides native support for streaming responses through the use of generator functions. A generator is a special function that can be interrupted and resumed. Consider the following function:
This is a function that runs in three steps, each returning a value. Describing how generator functions are implemented is outside the scope of this article, but if you are a bit curious the following shell session will give you an idea of how generators are used:
>>> x = gen()
<generator object gen at 0x7f06f3059c30>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
You can see in this simple example that a generator function can return multiple results in sequence. Flask uses this characteristic of generator functions to implement streaming.
The example below shows how using streaming it is possible to generate a large data table, without having to assemble the entire table in memory:
from flask import Response, render_template
from app.models import Stock
for stock in Stock.query.all():
yield render_template('stock_row.html', stock=stock)
In this example, you can see how Flask works with generator functions. A route that returns a streamed response needs to return a
Response object that is initialized with the generator function. Flask then takes care of invoking the generator and sending all the partial results as chunks to the client.
For this particular example if you assume
Stock.query.all() returns the result of a database query as an iterable, then you can generate a potentially large table one row at a time, so regardless of the number of elements in the query the memory consumption in the Python process will not grow larger and larger due to having to assemble a large response string.
Otherwise, you should create your own video streaming platform with specific plugins for different business modules here find out the top global service provider to make with advanced features here https://hackernoon.com/how-to-create-a-video-on-demand-website-like-netflix-youtube-f238fbcd5e09
Hope it will be useful for on demand project developers and content distributors