我在使用Firefox 12.0和GAE上的Python处理程序时遇到了一些奇怪的行为。在App Engine处理程序上接收来自Firefox 12.0的多个HTTP请求
当我在Firefox中请求这个处理程序时,它会运行3次 - 但只有当它返回一个GIF时。
我目前正在通过设置基于处理程序的查询字符串的memcache条目来绕过它。我希望这会防止相同信息的重复db.put()。
这里有一个工作URL:http://test-o-tron.appspot.com - 请注意,您可以更改这些查询字符串参数:
- 格式( “GIF” 或 “HTML”)
- 黑客( “真” 或 “假”)
- mkey_suffix(串在内存缓存键用于容易复位计数器)
下面的代码:
from google.appengine.api import urlfetch, memcache
from google.appengine.ext import db
import webapp2, random
class MainHandler(webapp2.RequestHandler):
def get(self):
#If user doesn't have an mkey_suffix, make one
if self.request.get("mkey_suffix") == "":
self.redirect("/?format=gif&hack=false&mkey_suffix=" +
self.request.remote_addr +
"." + str(random.randint(0, 1000)))
OUTPUT_GIF = self.request.get("format") == "gif"
USE_HACK = self.request.get("hack") == "true"
#Memcache keys
mkey_suffix = self.request.get("mkey_suffix")
mkey_log = "log" + mkey_suffix
mkey_hack = "hack" + mkey_suffix
#Count the number of requests using memcache
if memcache.get(mkey_log) is None:
memcache.set(mkey_log, 0, 60)
counter = memcache.get(mkey_log)
#Hack!! Only handle a given request ONCE every second
if not USE_HACK or memcache.get(mkey_hack) is None:
memcache.set(mkey_hack, True, time=1)
#Show I'm not crazy
counter += 1
memcache.set(mkey_log, counter, 60)
#Return counter value
if OUTPUT_GIF:
self.response.headers["Content-Type"] = "image/gif"
img_url = "http://placehold.it/{counter}x{counter}"
img_url = img_url.format(counter=str(400 + counter))
img_data = urlfetch.Fetch(img_url).content
content = db.Blob(img_data)
else:
#Output HTML
self.response.headers["Content-Type"] = "text/html"
content = "Counter == " + str(counter)
self.response.out.write(content)
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
“2013年6月17日美国/太平洋时间晚上10:38解决了多次执行Google App Engine请求的问题。我们对给您带来的不便表示歉意,并感谢您的耐心和持续的支持。请放心,系统可靠性是谷歌的首要任务,我们正在不断改进,以使我们的系统更好,一旦我们完成了内部调查,我们将对此事件进行更详细的分析。“ –