爱简单吧

分享简单,爱生活,爱简单

让Tornado 使用ujson

Tornado 默认使用json,但是它太慢了,我想用ujson 来代替。





在应用的入口多加下面的代码即可:

ujson
1
2
3
4
5
6
import sys

try:
    sys.modules['json'] = __import__('ujson')
except ImportError:
    pass
ujson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> import sys
>>> sys.modules['json'] = __import__('ujson')
>>> sys.modules['json']
<module 'ujson' from '/usr/local/lib/python2.7/dist-packages/ujson.so'>
>>> import json as a
>>> import ujson as b
>>> a == b
True
>>> a is b
True
>>> a
<module 'ujson' from '/usr/local/lib/python2.7/dist-packages/ujson.so'>
>>> b
<module 'ujson' from '/usr/local/lib/python2.7/dist-packages/ujson.so'>
>>>

 

Share

 

Comments