社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

利用gunicorn提高django的并发能力

yunson • 8 年前 • 1272 次点击  

原文链接

引言

    手头上的项目有一些采用django框架编写, 如果说并发量比较小的时候简单的
    runserver是可以应对的,那么当并发达到一两千的时候,该怎么提高django
    的并发能力呢?

Overview

  • 环境说明:
    • python: 3.5
    • django: 1.8.2
    • gunicorn: 19.7.1
  • 系统:

    • 服务器: centos 4核
    • 压测机器: centos 4核
  • 压测环境

    • siege
    • 4核centos测试机
  • 为什么用django

    • 开发效率高
    • 好上手
  • 关于gunicorn Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX. It's a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.(这是官方给出的回答)

本次实验业务场景

    post方式请求接口,然后往数据表中插入一条数据

压测方式及命令

  • 压测方式:lc
  • 压测命令:siege -c255 -t200S -v -b 'http://B_ip:8080/test POST appid=111'

代码展示

settings部分

    # 这里我们用mysql,其他配置都是默认
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'ce',
            'USER': 'root',
            'PASSWORD': '',
            'HOST': '192.168.96.95',
            'PORT': '3306',
            # 'CONN_MAX_AGE': 600,
        }
    }

models部分

    class Test(models.Model):
        url = models.CharField(max_length=228, blank=True, null=True)
        img_url = models.CharField(max_length=228, blank=True, null=True)
        title = models.CharField(max_length=228, blank=True, null=True)
        content = models.CharField(max_length=228, blank=True, null=True)

        class Meta:
            db_table = 'test'
            verbose_name = "test表"

        def __unicode__(self):
            return self.id

views部分

    class Test(APIView):

        def post(self, requsts):
            Test.objects.create(
                **{'url': str(1000000 * time.time())})
            return Response({"status": 200})

开始压测

数据说明

    目前数据库test表的数据量是, 其中id是自增主键
    MySQL [ce]> select id from test order by id desc limit 2;
    +--------+
    | id     |
    +--------+
    | 627775 |
    | 627774 |
    +--------+

runserver方式压测结果

    Lifting the server siege...      done.

    Transactions:              24041 hits
    Availability:              99.93 %
    Elapsed time:              99.60 secs
    Data transferred:           0.32 MB
    Response time:              1.03 secs
    Transaction rate:         241.38 trans/sec  # 并发量只有241
    Throughput:             0.00 MB/sec
    Concurrency:              248.94
    Successful transactions:       24041
    Failed transactions:              16
    Longest transaction:           32.55
    Shortest transaction:           0.05

gunicorn+gevent(4个worker)

    Lifting the server siege...      done.

    Transactions:              23056 hits
    Availability:             100.00 %
    Elapsed time:              99.49 secs
    Data transferred:           0.31 MB
    Response time:              1.09 secs
    Transaction rate:         231.74 trans/sec # 并发量只有231


    

    Throughput:             0.00 MB/sec
    Concurrency:              252.95
    Successful transactions:       23056
    Failed transactions:               0
    Longest transaction:            8.21
    Shortest transaction:           0.01

gunicorn+gthread(4个worker, --threads=50)

. . . 原文链接

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/2357
 
1272 次点击