Gunicorn: Question about IO bound process with gevent workers

Created on 15 Apr 2019  路  3Comments  路  Source: benoitc/gunicorn

Hello all,

I'm working on an API that sends several requests, gathers the results and combines the results with some processing and returns a response somewhere. I'm using the greenlet worker because the process is clearly IO Bound.

From what I've read each greenlet handling the gunicorn requests would act like a thread, so when ever a a IO blocking call has to wait for IO the greenlet will block for that particular gunicorn requests. So for example in the handler my app (using flask btw), I have several requests.get(...) the greenlet will block till next requests.get()

def fetch(pid):
    response0 = requests.get('http://some-micro-service-0.com/')
    response1 = requests.get('http://some-micro-service-1.com/')
    response2 = requests.get('http://some-micro-service-2.com/')
    return combine_response(response0, response1, response2)

I could use grequests to try to add more concurrency to the handler, to improve the latency (which is something I'm looking for). But do you have any suggestion on how to do this?

Do you have any suggestions on how to do the gather of several requests?

Most helpful comment

I could use grequests to try to add more concurrency to the handler,

grequests.map() is a shorthand version of the code snippet I provided above.

Other than that, grequests doesn't do much. It primarily exists to monkey-patch the process when it is imported, but gunicorn already does that.

All 3 comments

gevent has an example for dispatching multiple requests in parallel and proceeding only once they're done.

(Your case would be slightly different, you would return the response instead of printing it and then combine them.)

But we can actually go even simpler using the Group.map api:

from gevent import pool

jobs = pool.Group()
responses = jobs.map(requests.get, 
                     ('http://one', 'http://two', 'http://three'))

I could use grequests to try to add more concurrency to the handler,

grequests.map() is a shorthand version of the code snippet I provided above.

Other than that, grequests doesn't do much. It primarily exists to monkey-patch the process when it is imported, but gunicorn already does that.

closing the ticket since no activity happened on it since a while.

Was this page helpful?
0 / 5 - 0 ratings