我正在开发一个基于 GAE 和 python 2.7.13 的应用程序。我想要做的是在处理程序中进行一堆异步 API 调用。类似的东西: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.*为此,我已经试过像图书馆asyncio,grequests,requests和simple-requests,他们不似乎是工作,因为无论他们是不兼容GAE或python 2.7.13。有人能帮我一下吗?
1 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
默认情况下与 GAE 捆绑在一起的 Urlfetch有一种进行异步调用的方法:
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
如果由于某种原因您不想使用 urlfetch,您可以通过使用线程和同步队列来手动并行化请求以读取结果。
添加回答
举报
0/150
提交
取消