0

When I press play on any song in the media listing it has to get sent a send_file... however whilst the send_file is in progress, the whole website freezes meaning that if this send_file happens and someone goes to the main page of the website, they'll be waiting for as long as the send_file takes.

Here is my code:

@app.route('/getMedia/')
def getMedia():
    file = request.args.get('filename')

    def getReqFile(): 
        with open(f'./media/{file}', mode="rb") as file_like:
            yield from file_like 

    try:
        # send_file(f'./media/{file}', conditional=True, as_attachment=True)
        return Response(getReqFile())
    except FileNotFoundError:
        return 'The media you tried to view doesn\'t exist.'

I'm not sure how to word this better, so I'm sorry if it doesn't make sense.

WhatTheClown
  • 464
  • 1
  • 7
  • 24
  • 1
    Hi @HeyHoo, consider threading in backend or Service Worker in front end. – Michael Amadi Mar 06 '22 at 18:59
  • I was looking into threading but I couldn't find a way to merge it into my code without breaking it. Also, since I'm on Mac and don't want to do advanced stuff, I don't want to setup stuff like Redis or whatever – WhatTheClown Mar 06 '22 at 19:00

1 Answers1

1

Though usually overlooked this part can give you a head start

if __name__=="__main__":
    app.run(threaded=True)

Although you shouldn't depend entirely on it.

With threaded=True An implementation of SocketServer.ThreadingMixIn class

Handles each request in a new different thread. The number of thread that could be used for by the server to handle requests concurrently depends entirely on your OS and what limits it sets on the number of threads per process. So there's no limit, or rather, the limit of threads depends on your server capabilities.