GithubHelp home page GithubHelp logo

Comments (8)

miguelgrinberg avatar miguelgrinberg commented on June 18, 2024 1

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.

dougsouza avatar dougsouza commented on June 18, 2024 1

Finally, it worked. Thank you very much!!

from flask-video-streaming.

miguelgrinberg avatar miguelgrinberg commented on June 18, 2024

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 your app.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.

dougsouza avatar dougsouza commented on June 18, 2024

@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.

dougsouza avatar dougsouza commented on June 18, 2024

@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.

miguelgrinberg avatar miguelgrinberg commented on June 18, 2024

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.

dougsouza avatar dougsouza commented on June 18, 2024

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.

miguelgrinberg avatar miguelgrinberg commented on June 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.