user3715648
user3715648

Reputation: 1588

Is it possible to actually make async rest calls?

I need to make some async rest calls in a way that works in 2.7 and 3.x. I've seen some suggestions for grequests, but the documentation is pretty lacking. My default code looks like this:

import requests

for i in range(1, 10):        
    x = requests.post('some_endpoint', data={"a":i})

Works fine but isn't async. I tried using grequests, but the only usage i've found looks like this:

import grequests

for i in range(1, 10):
    x = grequests.post('some_endpoint', data={"a":i})
    grequests.map([x])

and this works, but its not acting async.

Am I doing something wrong, or does grequests not behave the way I was assuming? Is there some other library I can use that will work in 2.7 and 3.x?

Upvotes: 0

Views: 628

Answers (1)

oregano
oregano

Reputation: 854

You need to send all your requests at once using grequests.map as it won't return until all the requests are complete.

import grequests

req = []
for i in range(1, 10):
    req.append(grequests.get('https://www.google.com'))

grequests.map(req)
for r in req:
    print r.response

Upvotes: 1

Related Questions