使用Google App Engine的异步获取请求 [英] Asynchronous fetch request with Google App Engine

查看:120
本文介绍了使用Google App Engine的异步获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读GAE中异步提取请求的文档。 Python不是我的第一语言,所以我很难找出最适合我的情况。我并不真正需要或关心对请求的响应,我只需要它发送请求并忘记它,然后转到其他任务。



所以我尝试了像文档中的代码:

  from google .appengine.api import urlfetch 

rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc,http://www.google.com/)

#...做其他事情...

try:
result = rpc.get_result()
if result.status_code == 200:
text = result.content
#...
除了urlfetch.DownloadError:
#请求超时或失败。
#...

但是这段代码不起作用,除非我包含 try:,除了,我真的不在乎。虽然省略了该部分,但请求仍未完成。

在我不关心响应的情况下创建提取请求的最佳选择是什么,以便它只是开始请求,然后转到其他任何其他任务是,而且永不回头?

解决方案

只要做你的任务,其中的

 #...做其他事情... 

评论是。当你另外完成时,然后调用 rpc.wait()。请注意,它不是 try /除了,它使得它可以工作,它是 get_result()调用。可以用 wait()来替换。

所以你的代码看起来像这样:

  from google.appengine.api import urlfetch 

rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call( rpc,http://www.google.com/)

#...做其他事情...<<你的代码在这里

rpc.wait()


I'm reading the documentation for asynchronous fetch requests in GAE. Python isn't my first language, so I'm having trouble finding out what would be best for my case. I don't really need or care about the response for the request, I just need it to send the request and forget about it and move on to other tasks.

So I tried code like in the documentation:

from google.appengine.api import urlfetch

rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, "http://www.google.com/")

# ... do other things ...

try:
    result = rpc.get_result()
    if result.status_code == 200:
        text = result.content
        # ...
except urlfetch.DownloadError:
    # Request timed out or failed.
    # ...

But this code doesn't work unless I include try: and except, which I really don't care for. Omitting that part makes the request not go through though.

What is the best option for creating fetch requests where I don't care for the response, so that it just begins the request, and moves on to whatever other tasks there are, and never looks back?

解决方案

Just do your tasks where the

# ... do other things ...

comment is. When you are otherwise finished, then call rpc.wait(). Note that it's not the try/except that's making it work, it's the get_result() call. That can be replaced with wait().

So your code would look like this:

from google.appengine.api import urlfetch

rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, "http://www.google.com/")

# ... do other things ... << YOUR CODE HERE

rpc.wait()

这篇关于使用Google App Engine的异步获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆