Why python Flask API gives time differences in result while called by multiple users simultaneously?

62 views
Skip to first unread message

Gopi Pulithara

unread,
Aug 18, 2022, 4:36:25 AM8/18/22
to modwsgi
Hello my python file __init__.py contains
from flask import Flask
from waitress import serve

app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, Flask!"

@app.route("/GP")
def helloGP():
    return "Hello, Flask GP!"

@app.route("/test1a")
def testSalim():
    minute    = 100000000
    nDelay = 0
    for X in range(minute):
        nDelay = nDelay+1
    # return jsonify(data = nDelay)
    return str(nDelay)

if __name__ == "__main__":
    serve(app)

my wsgi file

#!/usr/bin/python
import sys
sys.path.insert(0,"C:/Apache24_Py/htdocs")
from helloworldapp import app as application

and in virtual host file
<VirtualHost *:5000>
    ServerName 127.0.0.1:5000
    ServerAlias 192.168.2.15:5000
    ErrorLog "logs/app2-error.log"
    CustomLog "logs/app2-access.log" common

   
    #WSGIDaemonProcess 127.0.0.1:5000 processes=2 threads=25
    WSGIScriptAlias / 'C:/Apache24_Py/htdocs/helloworldapp/helloworldapp.wsgi'
    Alias /static/ 'C:/Apache24_Py/htdocs/helloworldapp/static'
    <Directory 'C:/Apache24_Py/htdocs/helloworldapp/static'>
    Require all granted
    </Directory>
</VirtualHost>

While I call the api in postman with url

http://localhost:5000/test1a gives result in 8.69s

if this same api call by two users at a time the result will be 8.69s for one user and 15.35s for other user. Why is this like this ? Please help me to solve this. I am using Os windows 10 apache2.4 ad python 3.10.

I start the apache by calling httpd in the bin folder from cmd and call the url in postman with get method. My apache log shows this

[Thu Aug 18 12:15:13.617380 2022] [mpm_winnt:notice] [pid 18340:tid 420] AH00455: Apache/2.4.54 (Win64) OpenSSL/1.1.1p mod_fcgid/2.3.10-dev mod_wsgi/4.9.3 Python/3.10 PHP/8.1.9 configured -- resuming normal operations

But in my apache log its showing equal time for the response.

192.168.2.62 - - [18/Aug/2022:12:16:55 +0530] "GET /test1a HTTP/1.1" 200 9 192.168.2.15 - - [18/Aug/2022:12:16:55 +0530] "GET /test1a HTTP/1.1" 200 9

How can I solve this. please help and thanks in advance.

Graham Dumpleton

unread,
Aug 18, 2022, 5:06:41 AM8/18/22
to mod...@googlegroups.com
Because Python uses a global interpreter lock which means in effect that even when multithreading is used, that only one pure Python bit of code can run at a time.

In your case you are causing a CPU intensive task to occur in pure Python, without any callouts to anything which would cause that thread to give up control. Python will in that case still periodically pause the thread when executing in pure Python code, but you can still see a thread which is CPU intensive and pure Python code monopolise control and so when multiple requests they don't all get to equally run.

What I suggest you do is swap that CPU intensive code loop with a time.sleep() call instead and retest. In that case you should see the time take for each the same, as the time.sleep() is one of those points which will allow the Python interpreter to swap which thread is running and thus threads can interleave execution properly.

Anyway, go read up about the Python global interpreter lock and its impact on multithreading performance.

If you have a really intensive CPU task, then farm execution of it out into a separate process, although since you are using Windows that can get messy. On Linux there are other options, but on Windows you are probably more constrained on what you can do.

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/a977075b-8cee-4069-92a4-028d6f064505n%40googlegroups.com.

Gopi Pulithara

unread,
Aug 18, 2022, 6:49:42 AM8/18/22
to mod...@googlegroups.com
Thak you for the response and suggestion, will get back to you till I check what is GIL in python. Thank you once again.

Reply all
Reply to author
Forward
0 new messages