Tuesday, 22 June 2021

Flask app not working with multiple users simultaneously

Edit: Implementation video added on youtube I have added time stamps and some details in the description.

    @app.route("/")
    def index():
        return render_template("upload_pdf.html") # opens up a simple web page with file uploading form
    
    
    @app.route("upload_pdf", methods=["POST"])
    def upload_pdf():
        if request.method == "POST":
            # accepts pdf file from user and process it
            
            return render_template("upload_image.html") # opens up a simple web page with file uploading form
    
    
    @app.route("upload_image", methods=["POST"])
    def upload_image():
        if request.method == "POST":
    
            # brings up the processed pdf here
            # accepts image file here and process it with previously processed pdf
            
            send_file(pdf_bytes, download_name, as_attachment=True, mimetype='application/pdf')
    
    
    if __name__ == "__main__":
        app.run(debug=True)
    

Issue: When multiple users open up the link simultaneously and upload the files. Only the last file is selected as if only a single user is operating the app. Each file uploaded overwrites the previous one. Each click on the link is opening up a single session. We want a separate session for each user.

Sample Output 2 users simultaneously

 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment
.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 152-931-874
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [17/Jun/2021 09:54:59] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Jun/2021 09:54:59] "GET /static/js/script_cert.js HTTP/1.1" 304
-
127.0.0.1 - - [17/Jun/2021 09:54:59] "GET /static/styles/upload_cert.css HTTP/1.1"
 304 -
127.0.0.1 - - [17/Jun/2021 09:55:14] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Jun/2021 09:55:14] "GET /static/styles/upload_cert.css HTTP/1.1"
 304 -
127.0.0.1 - - [17/Jun/2021 09:55:14] "GET /static/js/script_cert.js HTTP/1.1" 304
-

Uploading a PDF file on each browser one by one

127.0.0.1 - - [17/Jun/2021 09:57:19] "POST /upload_pic HTTP/1.1" 200 -
127.0.0.1 - - [17/Jun/2021 09:57:19] "GET /static/js/script_pic.js HTTP/1.1" 304 -

127.0.0.1 - - [17/Jun/2021 09:57:19] "GET /static/styles/upload_pic.css HTTP/1.1"
304 -
127.0.0.1 - - [17/Jun/2021 09:58:00] "POST /upload_pic HTTP/1.1" 200 -
127.0.0.1 - - [17/Jun/2021 09:58:00] "GET /static/js/script_pic.js HTTP/1.1" 200 -

127.0.0.1 - - [17/Jun/2021 09:58:00] "GET /static/styles/upload_pic.css HTTP/1.1"
200 -

Things I tried so far:

  1. Without saving files and processed bytes only
  2. Saving files with a random file name at start.
  3. app.run(processes=4) Stackoverflow link
  4. Used gunicorn to host the app on Heroku. Same problem there.
  5. Tried multiprocessing locking but again same problem as the entire app is running in a single session it seems.
  6. List of other questions on Stackoverflow, I referred to, without any luck: Link1 Link2 Link3 Link4 Link5
  7. Here is the question which I suppose is closest to my problem, but it doesn't have any answer : Closest in terms of problem

I want to implement synchronization among all the users. How can I achieve this to have desired output?

Complete code on Github Github link. Please don't use drag and drop here as it is still under development.

I have no experience with flask. I am open to any clarification.



from Flask app not working with multiple users simultaneously

No comments:

Post a Comment