Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about IO bound process with gevent workers #2013

Closed
alep opened this issue Apr 15, 2019 · 3 comments
Closed

Question about IO bound process with gevent workers #2013

alep opened this issue Apr 15, 2019 · 3 comments

Comments

@alep
Copy link

alep commented Apr 15, 2019

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?

@jamadden
Copy link
Collaborator

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'))

@jamadden
Copy link
Collaborator

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.

@benoitc
Copy link
Owner

benoitc commented May 23, 2019

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

@benoitc benoitc closed this as completed May 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants