【问题标题】:Django + Gunicorn big TTFB timeDjango + Gunicorn 大 TTFB 时间
【发布时间】:2017-03-17 11:56:42
【问题描述】:

我正在查看 Google Chrome 控制台 ajax 请求时间。 我在后端测量,mysql 查询执行了 5 毫秒。 在 Chrome 控制台我看到this picture

TTFB 时间 333.07 ms。 我有 9 个 gunicorn 工人、Django 框架和 REST 框架。什么需要这么多时间?

例如, 我的看法:

@api_view(['GET'])
def get_gallery(request, slug):
    query = Gallery.objects.filter(route__slug=slug, route__is_active=True)

    return JSONResponse(GallerySerializer(query, many=True).data)


class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders its content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json; charset=utf-8'
        super(JSONResponse, self).__init__(content, **kwargs)

我的序列化器:

class GallerySerializer(ModelSerializerWithAuth):
    image = serializers.ImageField(use_url=False)
    thumb = serializers.ImageField(use_url=False)

    class Meta:
        model = Gallery
        fields = ('id', 'image', 'thumb')

gunicorn 配置:

bind = '127.0.0.1:9090'
errorlog = '/path/to/log'
timeout=120
user = 'user'

import multiprocessing
workers = multiprocessing.cpu_count() * 2 + 1

【问题讨论】:

  • 您是否尝试过使用分析器?
  • 我们应该如何在没有看到您的代码的情况下回答这个问题?我们应该使用水晶球吗?
  • @NilsWerner,什么是分析器?
  • 可以让您分析代码性能的东西。喜欢silk

标签: python django optimization time gunicorn


【解决方案1】:

可能是时候格式化答案了。默认情况下,响应在内存中构建并发送到客户端。

一个可能的解决方案/解决方法是使用StreamingHttpResponse

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2015-12-24
    • 2013-12-15
    • 2013-01-25
    • 2021-10-10
    相关资源
    最近更新 更多