亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 GAE 應用程序中執行異步 api 請求?

如何在 GAE 應用程序中執行異步 api 請求?

慕婉清6462132 2021-09-14 10:37:38
我正在開發一個基于 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,您可以通過使用線程和同步隊列來手動并行化請求以讀取結果。


查看完整回答
反對 回復 2021-09-14
  • 1 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號