Okay so this is strange and I am kinda lost and do not have a real explanation. But I will put my block of code here which runs extremely faster when ran directly from the terminal but becomes damn slow when the same piece of code is executed in a flask server hosted with gunicorn.
The code in question
start = time.time()
numbers = [row[0] for row in cur.fetchall()]
print(f"time taken for number campaign loop: {time.time() - start}")
start = time.time()
messages = [msg[0].replace('\n', '').replace('[URL]', full_domain[:-5] + get_random_string(5)) for msg in
message_res]
print(f"time taken for message campaign loop: {time.time() - start}")
Gunicorn command that is being used to serve the flask server:
gunicorn --workers=3 --reload --access-logfile '-' app:app --timeout 1800
- cur.fetchall() gives same data for both the tests I have made (terminal and inside flask)
- message_res also has same length in both the cases
Output from directly executing in the terminal:
time taken for number campaign loop: 0.0005254745483398438
time taken for message campaign loop: 0.15377187728881836
Output from when this function is called within a flask request:
time taken for number campaign loop: 0.00037980079650878906
time taken for message campaign loop: 93.94932818412781
Can anyone make me understand what am I missing here? My understanding was that no matter if a code is run directly inside a terminal or within a flask app the raw performance should not change and it should almost run at the same speed.
When I run it directly inside a terminal yes, it is in the same server where flask is hosted so I am not testing on two different machines.
I am using nginx too, if that matters. nginx routes requests on port 80 to gunicorn
Here is my nginx config inside sites-enabled
server {
listen 80;
server_name xx.xxx.xx.xx;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}