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