Django REST框架运行缓慢 [英] Django REST framework is slow

查看:131
本文介绍了Django REST框架运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Django REST框架进行生产准备,但不幸的是,它的执行速度很慢。

I'm started using the Django REST framework in preparation for production, but unfortunately, it is performing quite slowly.

我正在调用500个字典的数组,每个都有5个键值对。在外壳中,通话时间根本不明显-您按Enter键即可完成。以前,当我在没有REST框架的情况下直接提供相同的内容时,也没有明显的延迟。但是,使用REST框架,页面呈现后大约需要1-2秒才能显示内容。

I am calling an array of 500 dictionaries, each with 5 key-value pairs each. In the shell, the call-time is not noticeable at all - you press enter, and it's done. Previously, when I was serving the same content directly without the REST framework, there was also no noticeable delay. However, with the REST framework, it takes about 1 - 2 seconds after the page has rendered for the content to display.

我不认为这是由于javascript通过可浏览的API输入相同的详细信息会导致类似的延迟。

I do not think this is due to javascript as hitting the same details through the browseable API results in a similar delay.

此外,我现在正在缓存。

Also, I am NOT cacheing at the moment.

推荐答案

从问题中给出的详细信息来看,其他任何人都无法为您进行调试。

There's no way anyone else is going to be able to debug this for you from the details given in the question.


  • 您是重用现有的通用视图还是编写自己的视图?

  • 您是否正在序列化数据,如果是的话,该怎么做?您的序列化程序定义是什么样子?

  • 在呈现为JSON时还是仅在呈现为Browsable API时,问题会明显吗?

  • 您提到投放没有REST框架的内容-以前的视图是什么样的,之后的视图是什么样的?

  • Are you reusing an existing generic view or writing your own view?
  • Are you serializing the data, if so what does your serializer definition look like?
  • Does the issue manifest when rendering to JSON, or just when rendering to the Browsable API?
  • You mention serving the content without REST framework - what did the views looks like before, and what do they look like after?

REST框架的视图是 trivi al ,因此请使用配置文件工具,或直接覆盖它们并添加一些时间。同样,渲染器很简单-子类化当前使用的渲染器,覆盖 .render()并添加一些定时调用调用父级 .render()方法的一面。

The REST framework views are trivial, so use a profiling tool, or simply override them and add some timing. Likewise the renderers are trivial - subclass whatever renderer you're using at the moment, override .render() and add a couple of timing calls either side of calling the parent's .render() method.

如果您认为问题已经缩小到

If you think you've narrowed down a problem to a specific area then throw together a minimal test case and submit it as an issue.

序列化本身不太可能成为问题,我使用了相同的序列化引擎复制Django的夹具转储,并且没有明显的退化。

The serialization itself is unlikely to be an issue, I've used the same serialization engine to replicate Django's fixture dumping and there was no significant degradation.

可行的是,如果要跨模型关系进行查找,则可能需要添加构造查询集时,调用.select_related() .prefetch_related()调用,就像在其他Django视图中一样。

It's feasible that if you're doing lookups across model relationships you might need to add .select_related() or .prefetch_related() calls when constructing the queryset, exactly as you would with any other Django view.

编辑:请注意,在这篇文章之后,对序列化程序速度进行了重大改进,此票证中所述

Edit: Note that following on from this post there were significant serializer speed improvements made, as noted in this ticket.

这篇关于Django REST框架运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆