How can more than one client share the same generator? For the sake of simplicity I have just created a function that yields every lower letter. In my project I have a class that is being called that yields something else.
Folder structure:
app
---views
--------views.py
__init__.py
Suppose I have following __init__.py
in app/
.
from flask import Flask
def create_app():
app = Flask(__name__)
from .views.views import public as public_bp
app.register_blueprint(public_bp)
return app
And the simple view i app/views/views.py
import time, string
from flask import Blueprint, render_template, Response
public = Blueprint('views', __name__)
def generate():
letters = list(string.ascii_lowercase)
for l in letters:
yield f"var: {l} \r\n"
time.sleep(1)
@public.route('/')
def feed():
return Response(
generate(),
content_type='text/event-stream',
mimetype='multipart/x-mixed-replace'
)
Fire up the dev server and visit localhost:5000 and you will see the lower letter printed out.
How can another client enter where another client already is and not start the generate()
from the beginning?