PyPy3+uWSGI+Django 提升Django在大量并发下的运行效率

5 篇文章 0 订阅
5 篇文章 0 订阅

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的并发。
80并发,500次请求
运行10万次请求后

80并发,访问50000次
80并发,5万次请求
可以看到并发效率得到了极大提高,每秒能达到660

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值