1、編譯、安裝PyPy3(需要增加參數)
普通的安裝PyPy3的步驟,可以參考這篇博客 編譯PyPy3
由於需要使用uWSGI,這裏我們在編譯PyPy3的時候需要增加參數
python ../../rpython/bin/rpython -Ojit --shared --gcrootfinder=shadowstack pypy/goal/targetpypystandalone
各個參數的含義如下:
-Ojit - standard PyPy build with JIT
--shared - as expected, creates a .so
--gcrootfinder - on linux, the default rootfinder is asmgcc which has trouble with position independent code. This option is not needed on OS X or windows.
2、下載pypy_setup.py
uWSGI二進制中的pypy_setup或者uwsgi項目中的pypy_setup.py經過測試在PyPy3上是無法使用的。配置後可能會報如下的錯誤:
debug: OperationError:
debug: operror-type: SyntaxError
debug: operror-value: ("Missing parentheses in call to 'print'", ('c callback', 332, 14, ' print "PyPy WARNING: unable to load paste.script.util.logging_config"\n', 0))
或者
debug: OperationError:
debug: operror-type: TypeError
debug: operror-value: startswith first arg must be bytes or a tuple of bytes, not str
因此需要使用修改後的pypy_setup.py腳本,pypy_setup.py
3、配置uwsgi.ini
[uwsgi]
socket=127.0.0.1:8000
master=true
pidfile=/tmp/xxxxxxxxx-master.pid
daemonize=/var/log/uwsgi/xxxxxxxxxx.log
workers=40
buffer-size=32767
max-requests=50000
#使用pypy時,不需要使用plugin參數
pypy-lib=/home/root/pypy3.6-v7.0.0-src/pypy/goal/libpypy3-c.so
pypy-home=/home/root/pypy3.6-v7.0.0-src
pypy-wsgi-file=/path/to/yourproject/yourproject/wsgi.py
pypy-wsgi=[yourproject].wsgi:application
pypy-pp=/path/to/yourproject
pypy-setup=/path/to/pypy_setup.py
4、運行效率比較
使用PyPy3之後,當項目已經運行一段時間後,Django項目的效率會得到較大的提高,但是在剛開始啓動時,運行效率會低很多。這是由於PyPy3的JIT機製造成的,所以在訪問量比較小的程序上,是沒有必要去使用PyPy3的。
剛啓動時,80併發,訪問500次,可以看到運行效率比CPython還低,CPython一般來說能達到每秒200的併發。
運行10萬次請求後
80併發,訪問50000次
可以看到併發效率得到了極大提高,每秒能達到660