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")