0

I am working on an application which is based on GAE with python 2.7.13. What I want to do is that to make a bunch of async API calls inside a handler. Something like that:

class MakeRequests(webapp2.RequestHandler): def post(self, *v, **kv): *do an async api call#1* *do an async api call#2* *do an async api call#3* *wait for response from all of above api requests* *make response in a way like if call#1 failes, make it's expected* *attributes in response as None, if call#2 succeeds add it's* *attributes in response etc. This is just an example.* 

For that purpose, I have tried libraries like asyncio, grequests, requests and simple-requests, they don't seems to be working because either they are not compatible with with GAE or with python 2.7.13. Can anyone help me here?

1 Answer 1

2

Urlfetch, which is bundled by default with GAE has a way of making asynchronous calls:

from google.appengine.api import urlfetch def post(self, *v, **kv): rpcs = [] for url in urls: rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, url) rpcs.append(rpc) results = [rpc.get_result() for rpc in rpcs] # do stuff with results 

If, for some reason you don't want to use urlfetch you can parallelize the requests manually by using threading and a synchronized Queue to read the results.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. That really helped!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.