async_url_fetch 需要多长时间才能完成以及提供您的响应需要多长时间?
这是一种可能的方法来利用 api 在 python 中的工作方式。
需要考虑的几点。
回顾一下。
使用合理的截止日期和回调准备长时间运行的 url_fetch。使用 make_fetch_call 将其排入队列。为页面做你想做的工作。无论 url_fetch 是否完成或截止,都返回页面,无需等待。
GAE 中的底层 RPC 层都是异步的,似乎有一种更复杂的方式来选择您希望在工作中等待的内容。
这些示例使用 sleep 和 url_fetch 到同一应用程序的第二个实例。
wait() 调度 rpc 工作的示例:
class AsyncHandler(RequestHandler):
def get(self, sleepy=0.0):
_log.info("create rpc")
rpc = create_rpc()
_log.info("make fetch call")
# url will generate a 404
make_fetch_call(rpc, url="http://<my_app>.appspot.com/hereiam")
_log.info("sleep for %r", sleepy)
sleep(sleepy)
_log.info("wait")
rpc.wait()
_log.info("get_result")
rpc.get_result()
_log.info("return")
return "<BODY><H1>Holla %r</H1></BODY>" % sleepy
休眠 4 秒后调用等待显示调度
2011-03-23 17:08:35.673 /delay/4.0 200 4093ms 23cpu_ms 0kb Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16,gzip(gfe)
I 2011-03-23 17:08:31.583 create rpc
I 2011-03-23 17:08:31.583 make fetch call
I 2011-03-23 17:08:31.585 sleep for 4.0
I 2011-03-23 17:08:35.585 wait
I 2011-03-23 17:08:35.663 get_result
I 2011-03-23 17:08:35.663 return
I 2011-03-23 17:08:35.669 Saved; key: __appstats__:011500, part: 48 bytes, full: 4351 bytes, overhead: 0.000 + 0.006; link: http://<myapp>.appspot.com/_ah/stats/details?tim
2011-03-23 17:08:35.636 /hereiam 404 9ms 0cpu_ms 0kb AppEngine-Google; (+http://code.google.com/appengine; appid: s~<myapp>),gzip(gfe)
异步调度调用。
E 2011-03-23 17:08:35.632 404: Not Found Traceback (most recent call last): File "distlib/tipfy/__init__.py", line 430, in wsgi_app rv = self.dispatch(request) File "di
I 2011-03-23 17:08:35.634 Saved; key: __appstats__:015600, part: 27 bytes, full: 836 bytes, overhead: 0.000 + 0.002; link: http://<myapp>.appspot.com/_ah/stats/details?time
显示使用 memcache RPC 等待启动工作。
class AsyncHandler(RequestHandler):
def get(self, sleepy=0.0):
_log.info("create rpc")
rpc = create_rpc()
_log.info("make fetch call")
make_fetch_call(rpc, url="http://<myapp>.appspot.com/hereiam")
_log.info("sleep for %r", sleepy)
sleep(sleepy)
_log.info("memcache's wait")
memcache.get('foo')
_log.info("sleep again")
sleep(sleepy)
_log.info("return")
return "<BODY><H1>Holla %r</H1></BODY>" % sleepy
Appengine 产品日志:
2011-03-23 17:27:47.389 /delay/2.0 200 4018ms 23cpu_ms 0kb Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16,gzip(gfe)
I 2011-03-23 17:27:43.374 create rpc
I 2011-03-23 17:27:43.375 make fetch call
I 2011-03-23 17:27:43.377 sleep for 2.0
I 2011-03-23 17:27:45.378 memcache's wait
I 2011-03-23 17:27:45.382 sleep again
I 2011-03-23 17:27:47.382 return
W 2011-03-23 17:27:47.383 Found 1 RPC request(s) without matching response (presumably due to timeouts or other errors)
I 2011-03-23 17:27:47.386 Saved; key: __appstats__:063300, part: 66 bytes, full: 6869 bytes, overhead: 0.000 + 0.003; link: http://<myapp>.appspot.com/_ah/stats/details?tim
2011-03-23 17:27:45.452 /hereiam 404 10ms 0cpu_ms 0kb AppEngine-Google; (+http://code.google.com/appengine; appid: s~<myapp>),gzip(gfe)
当 memcache.get 调用 wait() 时分派异步 url 获取
E 2011-03-23 17:27:45.446 404: Not Found Traceback (most recent call last): File "distlib/tipfy/__init__.py", line 430, in wsgi_app rv = self.dispatch(request) File "di
I 2011-03-23 17:27:45.449 Saved; key: __appstats__:065400, part: 27 bytes, full: 835 bytes, overhead: 0.000 + 0.002; link: http://<myapp>.appspot.com/_ah/stats/details?time