项目中要得到页面的点击查看数,不知道如何实现。
先学习v2ex的思路,发现是用了GAE的taskqueue,我的理解是:在打开一个帖子时,同时会向/hit/topic/xxx这个地址发送请求(在任务队列中),把topic_key传过去,TopicHitHandler会根据topic_key去数据库中给hit字段加1.
from google.appengine.api.labs import taskqueue
#.....
taskqueue.add(url='/hit/topic/' + str(topic.key()))
#.....
class TopicHitHandler(webapp.RequestHandler):
def post(self, topic_key):
topic = db.get(db.Key(topic_key))
if topic:
topic.hits = topic.hits + 1
topic.put()
#.....
def main():
('/hit/topic/(.*)', TopicHitHandler)
后来看了web.py的文档,觉得web.py的background似乎和GAE的taskqueue差不多:
“web.background (and web.backgrounder) are python function decorators which allow you to execute a function in a separate background thread to that thread which served the current HTTP request and later report back on the status of the background thread (the stdout of the background function is in effect returned to the "backgrounder" that initiated the thread”
不知道我理解的对不对,不过看文档里似乎不建议用web.background——提到“handle with care”。这让我有点犹豫。
不知道除了用web.background之外,还有没有其他的办法来处理得到页面浏览数的问题?
先学习v2ex的思路,发现是用了GAE的taskqueue,我的理解是:在打开一个帖子时,同时会向/hit/topic/xxx这个地址发送请求(在任务队列中),把topic_key传过去,TopicHitHandler会根据topic_key去数据库中给hit字段加1.
from google.appengine.api.labs import taskqueue
#.....
taskqueue.add(url='/hit/topic/' + str(topic.key()))
#.....
class TopicHitHandler(webapp.RequestHandler):
def post(self, topic_key):
topic = db.get(db.Key(topic_key))
if topic:
topic.hits = topic.hits + 1
topic.put()
#.....
def main():
('/hit/topic/(.*)', TopicHitHandler)
后来看了web.py的文档,觉得web.py的background似乎和GAE的taskqueue差不多:
“web.background (and web.backgrounder) are python function decorators which allow you to execute a function in a separate background thread to that thread which served the current HTTP request and later report back on the status of the background thread (the stdout of the background function is in effect returned to the "backgrounder" that initiated the thread”
不知道我理解的对不对,不过看文档里似乎不建议用web.background——提到“handle with care”。这让我有点犹豫。
不知道除了用web.background之外,还有没有其他的办法来处理得到页面浏览数的问题?