当我们不关心结果时异步 URLfetch? [Python]
在我为 GAE 编写的一些代码中,我需要定期对另一个系统上的 URL 执行 GET,本质上是“ping”它,并且我不太关心请求是否失败、超时或成功。
因为我基本上想要“即发即忘”,而不是通过等待请求来减慢我自己的代码速度,所以我使用异步 urlfetch,而不是调用 get_result()。
在我的日志中,我收到一条警告:
发现 1 个 RPC 请求没有匹配的响应(可能是由于超时或其他错误)
我是否缺少一种明显更好的方法来执行此操作?在这种情况下,任务队列或延迟任务(对我来说)似乎有点矫枉过正。
任何意见将不胜感激。
In some code I'm writing for GAE I need to periodically perform a GET on a URL on another system, in essence 'pinging' it and I'm not terribly concerned if the request fails, times out or succeeds.
As I basically want to 'fire and forget' and not slow down my own code by waiting for the request, I'm using an asynchronous urlfetch, and not calling get_result().
In my log I get a warning:
Found 1 RPC request(s) without matching response (presumably due to timeouts or other errors)
Am I missing an obviously better way to do this? A Task Queue or Deferred Task seems (to me) like overkill in this instance.
Any input would appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任务队列任务是您的最佳选择。您在日志中看到的消息表明请求正在等待 URLFetch 完成后再返回,因此这没有帮助。你说任务“太过分了”,但实际上,它们非常轻量级,而且绝对是做到这一点的最佳方法。延迟甚至允许您直接延迟获取调用,而不必编写要调用的函数。
A task queue task is your best option here. The message you're seeing in the log indicates that the request is waiting for your URLFetch to complete before returning, so this doesn't help. You say a task is 'overkill', but really, they're very lightweight, and definitely the best way to do this. Deferred will even allow you to just defer the fetch call directly, rather than having to write a function to call.
async_url_fetch 需要多长时间才能完成以及提供响应需要多长时间?
这是一种利用 api 在 python 中工作方式的可能方法。
需要考虑的一些要点。
许多网络服务器和反向代理一旦启动就不会取消请求。因此,如果您正在 ping 的远程服务器提示请求但需要很长时间才能为其提供服务,请在 create_rpc(deadline=X) 上使用截止日期,以便 X 将因超时而返回。 ping 可能仍会成功。此技术也适用于 appengine 本身。
GAE RPC
回顾一下。
为长时间运行的 url_fetch 准备一个合理的截止日期和回调。使用 make_fetch_call 将其排队。完成您想要为页面做的工作。无论 url_fetch 是否完成或截止,都返回页面,并且无需等待。
GAE 中的底层 RPC 层都是异步的,似乎有一种更复杂的方法来选择您希望在工作中等待的内容。
这些示例使用 sleep 和 url_fetch 来访问同一应用程序的第二个实例。
wait() 调度 rpc 工作的示例:
休眠 4 秒后调用的 Wait 显示
异步调度调用的调度。
显示使用 memcache RPC 等待开始工作。
Appengine 产品日志:
当 memcache.get 调用 wait() 时调度异步 url 获取
How long does it take for the async_url_fetch to complete and how long does it take to provide your response?
Here is a possible approach to leverage the way the api works in python.
Some points to consider.
Many webservers and reverse proxies will not cancel a request once it has been started. So if your remote server you are pinging cues the request but takes a long time to service it, use a deadline on your create_rpc(deadline=X) such that X will return due to timeout. The ping may still succeed. This technique works against appengine itself as well.
GAE RPCs
To recap.
Prepare the long running url_fetch with a reasonable deadline and callback. Enqueue it using make_fetch_call. Do the work you wanted to for the page. Return the page regardless of wether the url_fetch completed or deadlined and without waiting for it.
The underlying RPC layer in GAE is all asynchronous, there seems to be a more sophisticated way to choose what you wish to wait on in the works.
These examples use sleep and a url_fetch to a second instance of the same app.
Example of wait() dispatching rpc work:
Wait called after sleeping for 4 seconds shows dispatch of
Async dispatched call.
Showing using a memcache RPC's wait to kick off the work.
Appengine Prod Log:
Async url fetch dispatched when memcache.get calls wait()