如何从异步请求应用程序引擎中的RPC对象获取响应URL?

时间:2011-10-24 20:18:30

标签: python google-app-engine asynchronous urlfetch

rpcs = []
for url in urls:
  rpc = urlfetch.create_rpc(deadline=5.0)
  urlfetch.make_fetch_call(rpc, url)
  rpcs.append(rpc)
while len(rpcs) > 0:
  rpc = apiproxy_stub_map.UserRPC.wait_any(rpcs)
  res = rpc.get_result()
  if res.status_code == 200:
    ...... do something with result
  rpcs.remove(rpc)

如何确定收到回复的网址?

1 个答案:

答案 0 :(得分:2)

使用回调:

def handle_result(rpc, url):
  ..... [your code goes here]

def create_callback(rpc, url):
  return lambda: handle_result(rpc, url)

rpcs = []
for url in urls:
  rpc = urlfetch.create_rpc(deadline=5.0)
  rpc.callback = create_callback(rpc, url)
  urlfetch.make_fetch_call(rpc, url)
  rpcs.append(rpc)

while rpcs:
  rpc = apiproxy_stub_map.UserRPC.wait_any(rpcs)
  rpcs.remove(rpc)