Home > OS >  send_file seems to pause the whole web server until the file is sent
send_file seems to pause the whole web server until the file is sent

Time:03-07

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.

CodePudding user response:

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.

  • Related