bottle.py中的路由搜索優化

# Now search regexp routes
# ROUTES_REGEXP是一個字典,鍵是請求方法,值是[路由, 處理函數]的列表
# 例如:{"GET", [[路由1, 處理函數1], [路由2, 處理函數2]]}
routes = ROUTES_REGEXP.get(method,[])
for i in xrange(len(routes)):
    match = routes[i][0].match(url)
    if match:
        handler = routes[i][1]
        if i > 0 and OPTIMIZER and random.random() <= 0.001:
          # Every 1000 requests, we swap the matching route with its predecessor.
          # Frequently used routes will slowly wander up the list.
          # 有千分之一的機率,能夠與前面的路由互換位置,這樣使用越頻繁的路由就會
          # 被換的越靠前,搜索起來效率就越高
          routes[i-1], routes[i] = routes[i], routes[i-1]
        return handler, match.groupdict()
raise HTTPError(404, "Not found")
相關文章
相關標籤/搜索