0

I have a very basic question. It seems like I don't get it, or I simply need a confirmation.

Let's say I set up a flask app and let it run using gunicorn. I use --wokers=2 and --threads=2, so I can serve 4 requests on parallel. Now let's say a client does 4 parallel requests, which will do a requests.get in the flask app, which needs 5 seconds to get a response (in theory). I fifth client call will need to wait for one of the 4 others to be finshed, before it's even started in the backend (and will take another 5 seconds for execution).

Now my question: When I switch to --worker-class gevent, will it help getting more parallel requests without adaping the code? If I understand it correctly, I need to properly use async library calls to get advantage of gevent, and to get a maximum parallel request execution of 1000 for example, right? Am I right by saying: If the code continues to simply do requests.get (or a sleep or whatever) without using async client libs, it the fifth request will still be blocked?

Thank you! (I've never worked with asnycio and coroutines, so I'm sorry)

Denny Weinberg
  • 2,492
  • 1
  • 21
  • 34
  • 1
    Yes it will. Synchronous functions don't have "pause", so your worker will be synchronously blocked for 5 seconds – sudden_appearance May 31 '22 at 18:59
  • @sudden_appearance I imagine I'm not the first asyncio beginner who needs to pay attention to that important fact. That should be highlighted in any tutorial, even if it seems logic when knowing a bit more about it. When reading the docs we could think it simply works out of the box. – Denny Weinberg May 31 '22 at 19:02
  • you need t o use async request calls, [this may help: using-requests-library-to-make-asynchronous-requests-with-python-3-7](https://stackoverflow.com/questions/51674751/using-requests-library-to-make-asynchronous-requests-with-python-3-7) or you can use a message queue – sahasrara62 May 31 '22 at 19:09
  • 1
    You don't need asyncio. Gevent will give this to you without changing your code, you just have to crank up the number of threads. Gevent works by monkeypatching the stdlib so that it will implicitly do a thread context switch any time you do IO (in fact any time GIL is released I think). That is the point of gevent, it implements lightweight "green" threads so you can launch lots of them – Anentropic May 31 '22 at 19:21
  • 1
    _"I need to properly use async library calls to get advantage of gevent"_ if you mean async/await or asyncio, then no. But you may need gevent-friendly versions of some libs that use C extensions... such as psycogreen for Postgres+psycopg2 – Anentropic May 31 '22 at 19:24
  • @Anentropic yes, postgres is a thing. But mainly http calls to jira and redmine are my problem because they can go into timeouts. They don't use c extensions I would say, only requests. So simply switching to gevent workers will do the trick because of the monkeypatching? – Denny Weinberg May 31 '22 at 19:31
  • I'm just seeing that gunicorn already does the monkeypatching patch_all by default. So year, requests should be fine than! – Denny Weinberg May 31 '22 at 19:49
  • 1
    Yes, you should experiment with thread numbers a bit to find the optimal number for your workload. But the key point is that gevent is designed to have many more threads than cpu cores, and to give you concurrency that way without having to change the code to async/await style (gevent vs asyncio are roughly "implicit" vs "explicit" concurrency, each has their pros and cons) – Anentropic Jun 01 '22 at 09:14

0 Answers0