Comments (8)
You can always restrict your camera thread and your streaming generator to deliver frames at a specific rate instead of going as fast as possible, that should give other tasks time to run. Just add a sleep inside their loops to slow them down a bit.
from flask-video-streaming.
Finally, it worked. Thank you very much!!
from flask-video-streaming.
You may have done something wrong, because gevent should have solved your problem.
But let's rewind a bit. These are your options to support concurrent requests:
-
With the Flask development web server, you can add
threaded=True
to yourapp.run()
call, or--with-threads
if you use the Flask CLI. -
With Gunicorn. Install it with pip, then start the server with
gunicorn --threads 5 --workers 1 --bind 0.0.0.0:5000 app:app
This will give you five concurrent requests (i.e. one stream and four more, or two streams and three more, etc.). You can change the 5 to the number that addresses your needs.
-
With Gunicorn and gevent. Install both with pip, then run with
gunicorn --worker-class gevent --workers 1 --bind 0.0.0.0:5000 app:app
This should give you up to 1000 concurrent requests.
-
Instead of gevent, you can also use eventlet. Install with pip, then run with
gunicorn --worker-class eventlet --workers 1 --bind 0.0.0.0:5000 app:app
This should also give you up to 1000 concurrent requests.
from flask-video-streaming.
@miguelgrinberg, indeed. I added a second endpoint to your code to test and it works. I'll figure it out what I am doing wrong. Thanks!
from flask-video-streaming.
@miguelgrinberg, the request does get processed, but it takes a lot of time (like 5 seconds, sometimes more) any idea where the bottleneck might be? I do some (heavy) image processing that I moved to your background thread, like this:
class Camera(BaseCamera):
@staticmethod
def frames():
camera = cv2.VideoCapture(0)
if not camera.isOpened():
raise RuntimeError('Could not start camera.')
styles = Styles() # this loads lots of stuff in GPU memory
current_style = 'la_muse'
while True:
# read current frame
_, frame = camera.read()
# this does some (heavy) GPU computation
frame = styles.convert_to_style(frame[:, :, ::-1], current_style)[:, :, ::-1]
frame = cv2.resize(frame, (640, 480))
frame = cv2.imencode('.jpg', frame)[1].tobytes()
# encode as a jpeg image and return it
yield frame
from flask-video-streaming.
Did you try the gunicorn approach without gevent? The async options require cooperative multitasking, they are not a great match for tasks that do heavy computing. Try with --threads 5
, I think that should work better since it will work with regular threads, which the OS knows how to schedule in and out of the CPU.
from flask-video-streaming.
It doesn't work. I make lots of calls to other endpoints, they take a huge time to process, if I refresh the browser they all come instantly (because the streaming has stopped for a second, I assume). I don't know what else to do. I'm considering splitting the app in two servers, one for the web app itself and regular APIs and other just for the streaming, and I would have to find a way to interact with the streaming server other than using API calls, of course.
from flask-video-streaming.
This issue will be automatically closed due to being inactive for more than six months. Seeing that I haven't responded to your last comment, it is quite possible that I have dropped the ball on this issue and I apologize about that. If that is the case, do not take the closing of the issue personally as it is an automated process doing it, just reopen it and I'll get back to you.
from flask-video-streaming.
Related Issues (20)
- How to send meta data Along with the image stream ? HOT 3
- The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a generator. HOT 3
- How does darknet yolo call the USB camera? HOT 1
- I need this to run in SSL HOT 1
- How to control the speed of video feed flow showing in the webpage? HOT 2
- Can we sync Flask streaming response with OpenCV FPS HOT 5
- IOError: [Errno 104] Connection reset by peer HOT 5
- Allways save the video as mp4 file? HOT 3
- I want to send a string alongwith each frame. HOT 4
- Loading the video stream freezes up all other responses from the Flask server HOT 4
- resetting yield generator during long run HOT 5
- Frames Are Displayed With Delay of One Frame
- Gstreamer backend
- Rotated Pictures HOT 1
- Hello, I don't know why the front-end console can receive a bunch of late results only after the back-end program is finished
- Publish the video through MotionEye HOT 6
- Picamera2/libcamera compatibility HOT 1
- Concurrency question HOT 3
- Stopping/Restarting video stream? HOT 4
- Return still image when requested HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask-video-streaming.