我在使用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請求的問題。我們對給您帶來的不便表示歉意,並感謝您的耐心和持續的支持。請放心,系統可靠性是谷歌的首要任務,我們正在不斷改進,以使我們的系統更好,一旦我們完成了內部調查,我們將對此事件進行更詳細的分析。「 –