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

Ascync non-blocking? #82

Closed
jakehilton opened this issue Feb 12, 2016 · 8 comments
Closed

Ascync non-blocking? #82

jakehilton opened this issue Feb 12, 2016 · 8 comments

Comments

@jakehilton
Copy link

Is it possible to initialize a pool to return the result but continue the code execution like so?

import grequests

def print_res(res, *args, **kwargs):
    from pprint import pprint
    pprint (vars(res))

print 'starting grequest'

req = grequests.post('http://www.cricket.com.au/', hooks=dict(response=print_res))
job = grequests.send(req, grequests.Pool(1))

print 'request made'

for i in range(10):
    print i

So that would show the request was made but still print the range while the request was in process and then finally print out the results of the send?

I'd really love to use grequests but the examples/documentation are fairly sparse so I'm struggling.

Thanks for any help.

@kennethreitz
Copy link
Collaborator

You should be able to use grequests.get(..., callback=print_res).

@jakehilton
Copy link
Author

The problem is not that the callback wasn't being called.. it's that it's blocking on the .send. Am I misunderstanding that it shouldn't block when using the Pool? I would expect it to log something like so:

starting grequest
request made
1
2
3
<http content back>
4
5
6
7
8
9
10

Instead it blocks and the content is printed out before the request made log is logged.

@RavisMsk
Copy link

RavisMsk commented Apr 21, 2016

@jakehilton

Hey Jake,

This library is blocking calling thread, so it's not as asynchronous as you could think. It's like half-asynchronous, dunno how to call that...
I think lots of people actually think it'll send the HTTP task to separate thread-pool and eventually call back with results, but it doesn't. Yes, it sends multiple requests, which are asynchronous TO EACH OTHER, and awaits them (https://github.com/kennethreitz/grequests/blob/master/grequests.py#L117).
And that is really confusing. I think authors should clarify that in docs.

@Lythimus
Copy link

@RavisMsk Jake isn't using the map function, he's using the send function, which is supposedly asynchronous, unlike map which is only async in that it will block on receiving responses from a group of async requests.

@svenyonson
Copy link

Any updates on this? I need to send a single request, asynchronously, so as not to block the main thread, and be called back when the request completes.

@svenyonson
Copy link

I'm getting the same results as Jake. My post request is blocking.
xyz = grequests.post('https://xyz.com/', data = data, headers = headers, callback=my_reply).send()

@kennethreitz
Copy link
Collaborator

this library is not actively maintained.

@kennethreitz
Copy link
Collaborator

From the readme: Note: You should probably use requests-threads or requests-futures instead.

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

5 participants