我正在查看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发布于 2017-03-17 20:15:06
可能是时候格式化答案了。默认情况下,响应是在内存中构建的,然后发送到客户端。
一种可能的解决方案/解决方法是使用StreamingHttpResponse
https://stackoverflow.com/questions/42856961
复制相似问题