Please help me if you can, I am py_level = "nu-bie",
Problem description: can't obtain any kind of "multi-tasking", in my own PC, in Flask application ( not with Flask, not with Gunicorn, not with Gevent ).
Environment:
* Python version: 3.10.13
* Flask version: 3.0.1
* Ubuntu 20.04
Assume I have a Flask application where one of the "branches is:"
import time
from flask import Flask
app = Flask(__name__)
@app.route('/slow')
def slow_branch():
for i in range( 0,15 ):
time.sleep(1)
return "finished"
( the function takes 15 seconds to finish )
The app is started with Flask, at the Terminal:
$flask --app myapp --host=localhost --port=5000
Now, in my own PC, I open 2 web-browsers, and point both of them to:
http://localhost:5000/slow
As fast as I can, I press [ENTER] on both browsers. The first browser finishes in 15 secords, as expected. The second browser takes 32 seconds to finish ( 15 waiting + 15 function time + 2 seconds delay from my part, to go from browser 1 to browser 2 ).
My conclussion: There is no multi-tasking taking place.
I try a "front-end" Gunicorn with
$gunicorn -w 4 -b 127.0.0.1:5000 myapp:app
Same thing. 32 seconds for the second browser to finish. No multi-tasking.
I try Gevent, with a file containing:
from gevent.pywsgi import WSGIServer
from myapp import app
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
This file is named: [gevent.start] Then, from command line:
$python gevent.start
( I have a virtual environment, so [python] starts a python3.10.3 )
It starts the server ok, but no multi-tasking.
The first browser finishes in 15 secords,
The second browser takes 32 seconds to finish ( 15 + 15 + 2 seconds delay from my part ).
What am I doing wrong ?
Why workers are not working ?
How can multi-tasking be achieved ? ( Both browsers should finish in 15-16 seconds total time, because running in "parallel" ).
I was thinking... (!) Should slow_branch() start a thread to do the work ? In this way slow_branch returns almost immediately. But the "Request" object will probably be destroyed by Flask, and my thread will need it (I think). So complicated ! Do I need to write a socket application from scratch to do this multi-tasking ?
Thank you in advance for your kind help.