gpt4 book ai didi

python - django-debug-toolbar-line-profiler 只显示单行输出,没有内容

转载 作者:太空狗 更新时间:2023-10-29 19:29:38 25 4
gpt4 key购买 nike

我有一个 Raspberry Pi 位于远程位置。它连接到一个小型自制电路和一个温度探头。我设置了 Raspberry Pi 来做一些事情:

  • 每小时运行一次 cron 作业以获取温度读数并将其存储在本地的 sqlite 数据库中
  • 运行 Nginx 网络服务器
  • 运行 uwsgi 应用服务器
  • 提供一个简单的 Django 应用

在那个 Django 应用程序中,我有一个执行以下操作的简单 View :

  1. 点击数据库获取最近 300 条温度记录
  2. 将它们放入 Pandas DataFrame
  3. 使用 Matplotlib 生成近期温度历史的精美 SVG 图表
  4. 填写一个简单的模板,该模板显示 SVG 以及最近温度读数的小型 HTML 表格。

渲染此 View 大约需要 30 秒。很长一段时间。所以我想看看是什么花了这么长时间。我猜这是与生成图形相关的所有工作。但为了找出答案,我想做一些分析。

我使用 pip 安装了 django-debug-toolbardjango-debug-toolbar-line-profiler

我已尽我所能根据文档配置它们。特别是,我设置了:

DEBUG = True
TEMPLATE_DEBUG = DEBUG
DEBUG_TOOLBAR_PATCH_SETTINGS = False

MIDDLEWARE_CLASSES = (
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

DEBUG_TOOLBAR_PANELS = (
'debug_toolbar.panels.versions.VersionsPanel',
'debug_toolbar.panels.timer.TimerPanel',
'debug_toolbar.panels.settings.SettingsPanel',
'debug_toolbar.panels.headers.HeadersPanel',
'debug_toolbar.panels.sql.SQLPanel',
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
'debug_toolbar.panels.templates.TemplatesPanel',
'debug_toolbar.panels.cache.CachePanel',
'debug_toolbar.panels.signals.SignalsPanel',
'debug_toolbar.panels.logging.LoggingPanel',
'debug_toolbar.panels.redirects.RedirectsPanel',

'debug_toolbar_line_profiler.panel.ProfilingPanel',
)

此外,INTERNAL_IPS也设置妥当。

我使用基于类的 View 构建了我的 View 。它看起来像这样:

from django.views.generic import TemplateView
from XXXX.models import TempReading, TempSeries
import numpy as np
import pandas as pd
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import seaborn as sbn
import StringIO

class TestView(TemplateView):
template_name = 'XXXX/test.html'

def get_context_data(self, **kwargs):
upstairs = TempSeries.objects.get(name='Upstairs')
upstairstemps = upstairs.tempreading_set.all().order_by('-timestamp')[:300]

frame = pd.DataFrame(list(upstairstemps.values()))
frame.set_index('timestamp', inplace=True)

# matplotlib.rcParams['svg.fonttype'] = 'none'

fig = Figure()
ax = fig.add_subplot(1,1,1)
frame['value'].plot(ax=ax)
ax.get_xaxis().grid(color='w', linewidth=1)
ax.get_yaxis().grid(color='w', linewidth=1)

fig.set(facecolor='w')
canvas = FigureCanvas(fig)

imgdata = StringIO.StringIO()
canvas.print_svg(imgdata)

imgstr = imgdata.getvalue()

context = super(TestView, self).get_context_data(**kwargs)
context['svgtext'] = imgstr
context['htmltable'] = frame[:5].to_html()

return context

我最感兴趣的代码是get_context_data

当我加载页面时,调试工具栏确实出现了。并显示分析面板。但我所看到的是:

{method 'disable' of '_lsprof.Profiler' objects}

这是页面首次加载时的屏幕截图: enter image description here

这是它在分析页面上的样子: enter image description here

它似乎根本没有在进行任何“线路分析”!我期待在基于类的 View 中看到每一行的定时结果。特别是对于 get_context_data 函数中的每一行。这是怎么回事?非常感谢任何帮助。


4/2 编辑

作为测试,我编写了一个虚拟 View ,使用基于类的 View 。这似乎工作得很好。这是新的非基于类的 View :

def testview2(request):
df = pd.DataFrame({'a': np.random.randn(10), 'b': np.random.randn(10)})
htmltable = df.to_html()
context = {}
context['htmltable'] = htmltable

return render(request, 'XXXX/test2.html', context)

这会在分析 Pane 中产生以下结果: enter image description here

所以这似乎工作正常。关于 debug-toolbar-line-profiler 如何与基于类的 View 一起工作,我是否遗漏了一些微妙之处?在文档中,它建议它将分析类中不以下划线开头的任何方法。这是不正确的吗?

最佳答案

当我用 @csrf_exempt 装饰我的 View 时,我注意到了这种情况;一旦移除,探查器就可以正常工作。

不确定为什么会导致这个问题,但这对我来说已经解决了。

关于python - django-debug-toolbar-line-profiler 只显示单行输出,没有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22748940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com