Skip to content
This repository
Newer
Older
100644 33 lines (22 sloc) 0.838 kb
f2e74475 » ohsc
2010-01-20 edit
1 ---
2 layout: default
3 title: Application processors
4 ---
5
6 # Application processors
7
8 ## 问题
9
10 如何使用应用处理器,加载钩子(loadhooks)和卸载钩子(unloadhook)?
11
12 ## 解法
13
14 web.py可以在处理请求之前或之后,通过添加处理器(processor)来完成某些操作。
15
16 def my_processor(handler):
17 print 'before handling'
18 result = handler()
19 print 'after handling'
20 return result
21
22 app.add_processor(my_processor)
23
24 可以用加载钩子(loadhook)和卸载钩子(unloadhook)的方式来完成同样的操作,它们分别在请求开始之前和结束之后工作。
25
26 def my_loadhook():
27 print "my load hook"
28
29 def my_unloadhook():
30 print "my unload hook"
31
32 app.add_processor(web.loadhook(my_loadhook))
33 app.add_processor(web.unloadhook(my_unloadhook))
Something went wrong with that request. Please try again.