【问题标题】:django-rest-framework speed-up endpoint with http requests带有 http 请求的 django-rest-framework 加速端点
【发布时间】:2022-06-18 15:51:28
【问题描述】:

我在 DRF 上有一个应用程序,其中有一个端点,它在循环中为每个项目发送一个 http 请求。
端点因此非常慢,有什么想法可以加快速度吗?
代码示例

class MyView(APIView):
    def get(self, request: Request) -> Response:
        for cat in Cats.objects.all():
            data = CatsInfoService.get_info(cat) # send http request
        return Response({"message": "ok"})

【问题讨论】:

    标签: python django backend


    【解决方案1】:

    您是否尝试过使用这样的迭代器?

    class MyView(APIView):
        def get(self, request: Request) -> Response:
            for cat in Cats.objects.all().iterator():
                data = CatsInfoService.get_info(cat)
            return Response({"message": "ok"})
    

    另外,我认为这篇文章也可以提供帮助Optimize Django Queryset for loop

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 2013-03-12
      相关资源
      最近更新 更多