3

I have a monkey patched flask api with gevent that does not seem to process requests in parallel.

tested with gevent version = 1.4.0; python 2.7 and python 3.7

(I also tried with gevent.sleep, still does not work)

from gevent import monkey
monkey.patch_all()
from flask import Flask
from gevent.pywsgi import WSGIServer
import gevent
import time
app = Flask(__name__)

@app.route('/')
def overview():
    print('1')
    time.sleep(10)
    print('2')
    return "ok"


WSGIServer(('', 3341), app).serve_forever()

Output for two requests sent via different tabs in browser:

1
2
::1 - - [2020-01-09 16:51:42] "GET / HTTP/1.1" 200 117 10.006179
1
2
::1 - - [2020-01-09 16:51:52] "GET / HTTP/1.1" 200 117 10.005313

shows that the server is processing requests sequentially.

Thanks for helping.

Mohit Chawla
  • 181
  • 7
  • I think you missed something. Everything works fine. – Danila Ganchar Jan 10 '20 at 13:35
  • @DanilaGanchar, thanks for replying. Do you mean the expected output looks fine to you or do you mean the output in your system looks fine to you ? I was expecting something like: 1 1 2 --> response 200 for first request 2 --> response 200 for second request – Mohit Chawla Jan 13 '20 at 21:53
  • I mean I can't reproduce. I got expected result. – Danila Ganchar Jan 14 '20 at 07:53
  • Can you please tell me, your gevent version, flask version, python version and how did you send two requests ? Thanks. – Mohit Chawla Jan 14 '20 at 23:39
  • `gevent 1.4.0`, `Flask 1.1.1`, `python 3.6`. 1 worker(just your script `python test.py`). [checked one more time](https://prnt.sc/qnx2iz). I think you just missing something. – Danila Ganchar Jan 15 '20 at 08:50
  • okay, thanks for replying, i will try to figure out what am i missing. I hope sending requests via two browser tabs should not affect things. – Mohit Chawla Jan 15 '20 at 17:19
  • `browser` / `curl` / `postman` / something else doesn't matter. by the way try to run app using `gunicorn` + `gevent`. just for experiment(`gunicorn test:app -k gevent -w 1`) – Danila Ganchar Jan 15 '20 at 18:16
  • I have the same issue... @mohitchawla did you find a fix? `gevent 21.12.0` `gevent-websocket 0.10.1` `Flask 2.0.2.0` and `Python 3.9.2` – nmz787 Jul 14 '22 at 19:31

1 Answers1

0

I found the Chrome browser was the culprit, after learning based on this answer: https://stackoverflow.com/a/62912019/253127

basically Chrome is trying to cache the result of the first request, and then serve that to the additional tabs. Oddly it seemed like only my first incognito window worked, and then subsequent incognito windows also stalled.

So overall, using a window of each:

  • normal Chrome
  • Chrome incognito
  • MS Edge

I finally saw 3 immediate print('1') statements show up on stdout

nmz787
  • 1,960
  • 1
  • 21
  • 35