服务器响应时间比预期的长得多

2024-09-29 00:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Django应用程序,目前在本地主机上运行。有些页面需要更长的时间,因此出于调试原因,我使用以下简单代码计算了在一个基于类的视图中花费的时间:

class DiscoverView(LoginRequiredMixin, ListView):
    model = MyModel
    template_name = "app1/discover.html"
    context_object_name = 'influencers'  # not necessary, default is object_list

    def get_context_data(self, **kwargs):

        start_time = time.time()
        # ...
        # Some tasks which I tought they are the reasons of long response time
        # ...
        print(time.time() - start_time, "seconds took to complete...")
        return context

在实验之后,我发现这个视图需要0.01秒才能完成,而页面加载需要大约5秒

为了进一步调试,我深入研究了Chrome开发工具。在网络选项卡中,页面的“完成”时间为5.86秒。我还运行了Lighthouse测试,该测试表明服务器的初始响应时间约为2秒

我不明白为什么这0.01秒变为2秒,我还使用了django调试工具栏来检查数据库查询时间,这并不长。我的应用程序也在生产环境(Heroku)中运行,加载时间当然要糟糕得多,但我觉得我需要首先在本地解决这个问题

我感谢您提供的任何帮助或调试建议,如果您需要有关应用程序/系统的更多信息,请告诉我

编辑:这里是get_context_data()的内部:

def get_context_data(self, **kwargs):
        start_time = time.time()

        context = super().get_context_data(**kwargs)
        platform = self.kwargs['platform']
        page_no = self.request.GET.get('page') # make it int to multiply when slicing
        if page_no is not None: page_no = int(page_no)
        ITEMS_PER_PAGE = 10
        parents = None
        
        if platform == 'instagram':
            # Will implement this later
            pass
        elif platform == 'tiktok':
            tiktok_influencers = TiktokInfluencer.objects.all()
            context['platform_counter'] = tiktok_influencers.count()
            parents = InfluencerParent.objects.filter(tiktok__id__in = tiktok_influencers)
        elif platform == 'youtube':
            youtube_influencers = YoutubeInfluencer.objects.all()
            context['platform_counter'] = youtube_influencers.count()
            parents = InfluencerParent.objects.filter(youtube__id__in = youtube_influencers)
        elif platform == 'twitch':
            twitch_influencers = TwitchInfluencer.objects.all()
            context['platform_counter'] = twitch_influencers.count()
            parents = InfluencerParent.objects.filter(twitch__id__in = twitch_influencers)
        elif platform == 'clubhouse':
            clubhouse_influencers = ClubhouseInfluencer.objects.all()
            context['platform_counter'] = clubhouse_influencers.count()
            parents = InfluencerParent.objects.filter(clubhouse__id__in = clubhouse_influencers)
        else:
            # unknown platform, redirect to 404
            pass
        
        # Pagination stuff
        if page_no == None or page_no == 1:
            parents = parents[:ITEMS_PER_PAGE]
        else:
            parents = parents[(page_no-1) * ITEMS_PER_PAGE : (page_no) * ITEMS_PER_PAGE]

        context['influencers'] = parents
        context['platform'] = platform

        print(time.time() - start_time, "seconds took DiscoverView to complete...")
        return context

编辑2:我需要更多帮助,因此我正在添加模型和模板信息:

# models.py
class InfluencerParent(models.Model):
'''
Parent class which points to influencers' IG, YT & other social media platform accounts
'''
    
def __str__(self):
    if self.instagram.first():
        return self.instagram.first().fullname
    else:
        return "None"

class InstagramInfluencer(models.Model):
    # some fields...
    influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)

class YoutubeInfluencer(models.Model):
    # some fields...
    influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)

class TiktokInfluencer(models.Model):
    # some fields...
    influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)

class TwitchInfluencer(models.Model):
    # some fields...
    influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)

class ClubhouseInfluencer(models.Model):
    # some fields...
    influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)

模板示例:

# template (not the all for the sake of clarity)
<!-- discover.html

A single template to list all influencers (IG, YT, Tiktok...)
Takes platform param from URL
In view we filtered parents according to the platform
For example if platform param is 'tiktok' I use this updated version:   
parents = InfluencerParent.objects.filter(tiktok__isnull=False).prefetch_related('instagram', 'youtube', 'tiktok', 'twitch', 'clubhouse')

There parents influencers are who have tiktok accounts, in template I show all the social media cards in a tabbed design
So I need to send parent and acces parent.instagram.first.username
-->

{% for influencer in influencers %}

<div class="instagram" {% influencer.instagram.first == None %} disabled {% endif %}>
    {{influencer.instagram.first.username}}
</div>

<div class="tiktok" {% influencer.tiktok.first == None %} disabled {% endif %}>
    {{influencer.tiktok.first.username}}
</div>

<!-- 
. 
. 
OTHER SOCIAL MEDDIA ACCOUNTS OF THAT PARENT
. 
. 
-->

<div class="clubhouse" {% influencer.clubhouse.first == None %} disabled {% endif %}>
    {{influencer.clubhouse.first.username}}
</div>

以下是我如何使用预回迁相关(选择相关错误):

elif platform == 'tiktok':
    context['platform_counter'] = TiktokInfluencer.objects.count()
    parents = InfluencerParent.objects.filter(tiktok__isnull=False).prefetch_related('instagram', 'youtube', 'tiktok', 'twitch', 'clubhouse')
    # This is how I use prefetch_lated(), I need to send parent objecjts and their all (if exists) platform related influencer objects
    # I acces Parent's all existing reverse foreign key fields in one template, for example:
    # {{parent.influencer.username}}
    # {{parent.youtube.follower_count}}
    # {{parent.tiktok.fullname}}
    # ...

    # The row below gives error: 'Invalid field name(s) given in select_related: 'tiktok'. Choices are: (none)'
    # Even tho I need  to give all social platforms as params (select_related('instagram', 'youtube', 'tiktok', 'twitch', 'clubhouse'))
    parents = InfluencerParent.objects.select_related('tiktok')

Tags: noneobjectstimemodelscontextclassinstagramparent
1条回答
网友
1楼 · 发布于 2024-09-29 00:22:46

https://docs.djangoproject.com/en/3.2/ref/models/querysets/ 有助于建立一个基础“强> >选择相关的< /强>,对于具有外键关系的模型,通常对于M2M的模型来说,<强> > PrimthChuy相关。 在这里,我将提供一种“简单”的方法来实现与预取相关的。请记住,连接到数据库的调用不仅在views.py中,而且在模板中,访问属性可能会对数据库造成额外的影响(请参阅上面的链接以获得进一步的解释,即blog.author.homely)。因此,为了减少我们访问数据库的次数,我建议实现如下内容。现在,在没有看到模型、模板和实际SQL查询的情况下,这将是我最好的猜测

        
        ...
        if platform == 'instagram':
            # Will implement this later
            pass
        elif platform == 'tiktok':
            context['platform_counter'] = TiktokInfluencer.objects.count()
            parents = InfluencerParent.objects.prefetch_related('tiktok')
        elif platform == 'youtube':
            context['platform_counter'] = YoutubeInfluencer.objects.count()
            parents = InfluencerParent.objects.prefetch_related('youtube')

         ... repeat this pattern for others
        else:

我认为,只要添加我所做的编辑,就会大大改变查询量

如果你想知道它将如何改变。获取部分视图,并在shell中进行一些测试

from django.db import reset_queries
from django.db.models import connections

>>> parents = list(InfluencerParent.objects.prefetch_related('tiktok'))
>>> connections.queries
>>> reset_queries()
>>> parents = list(InfluencerParent.objects.select_related('tiktok')) 
>>> connections.queries
# If both work, then your using a Forgeign Key but more importantly
# notice how many SQL queries are made. If using select_related breaks 
# its because your using an M2M. But again, the importants is getting that list of "connections" as low as possible

现在,为了进一步完善您的代码,我将看一看预回迁,因为您可以使缓存查询与之前实现的内容类似

from django.db.models import Prefetch

>>> qs = MyModel.objects.filter(some_lookup)
>>> results = MyDesiredResultObjects.objects.prefetch_related(Prefetch('some_attr'), queryset=qs, to_attr='my_new_result')
>>> my_new_result

=====================================
因为您已经提供了型号
选择相关的

...
elif platform == 'tiktok':
            context['platform_counter'] = TiktokInfluencer.objects.count()
            parents = InfluencerParent.objects.select_related('tiktok')

相关问题 更多 >