I am developing an application which streams camera to a web page and I was following the article at https://tree.rocks/a-simple-way-for-motion-jpeg-in-flask-806b8bfefa96
As given in the tutorial, I started with a simple animation and wrote the following code using Flask and Waitress
app = Flask(__name__)
def gather_img():
while True:
time.sleep(0.2)
img = np.random.randint(0, 255, size=(128, 128, 3), dtype=np.uint8)
_, frame = cv2.imencode('.jpg', img)
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + frame.tobytes() + b'\r\n')
@app.route("/get-image")
def mjpeg():
return Response(gather_img(), mimetype='multipart/x-mixed-replace; boundary=frame')
serve(app)
Then I ran the program using following command
waitress-serve --listen=*:8080 img:create_app
But at a time, I can open maximum 4 connections (4 tabs) to the server. Then I added threads parameter to increase the number of worker threads
waitress-serve --listen=*:8080 --threads=24 img:create_app
But even then I could open only 4 connections max. I have the following questions
- What am I missing here? Why can't I open more than 4 connections to the waitress server even though I increased number of worker threads?
- Is there a better alternative to stream series of images (live video) to a web page?
Note: I already tried HLS, but then I found that it was having 5-6 seconds delay in streaming and is intended mainly for playback and not live streaming.