我在使用line_profiler分析金字塔视图时遇到了问题。由于金字塔的@view_config
不能与@profile
一起使用,所以我修改了我用于其他目的的视图装饰器:
def view_decorator_profile(view_callable):
def inner(context, request):
request._mso = MySpecialObject()
log.debug('################## VIEW_CALLABLE %s', view_callable)
profile.add_function(view_callable)
return view_callable(context, request)
return inner
我以标准的金字塔形式使用它:
@view_config(decorator=view_decorator_profile, route_name='myroute', renderer='....', permission='...')
def my_callable(...):
现在,分析运行..除了金字塔似乎做了一些黑魔法而不是my_callable
被分析,rendered_view
被分析了!
请参阅:
Function: rendered_view at line 353
Line # Hits Time Per Hit % Time Line Contents
==============================================================
353 def rendered_view(context, request):
354 renderer = view_renderer
355 result = view(context, request)
356 if result.__class__ is Response: # potential common case
357 response = result
358 else:
359 registry = self.registry
360 # this must adapt, it can't do a simple interface check
361 # (avoid trying to render webob responses)
362 response = registry.queryAdapterOrSelf(result, IResponse)
363 if response is None:
364 attrs = getattr(request, '__dict__', {})
365 if 'override_renderer' in attrs:
366 # renderer overridden by newrequest event or other
367 renderer_name = attrs.pop('override_renderer')
368 renderer = renderers.RendererHelper(
369 name=renderer_name,
370 package=self.kw.get('package'),
371 registry = registry)
372 if '__view__' in attrs:
373 view_inst = attrs.pop('__view__')
374 else:
375 view_inst = getattr(view, '__original_view__', view)
376 response = renderer.render_view(request, result, view_inst,
377 context)
378 return response
2016-02-23 17:43:34,259 INFO base.py:159 shutdown | Scheduler has been shut down
2016-02-23 17:43:34,288 INFO __init__.py:195 atexit_handler |
现在,我很肯定我在装饰器中获得的view_callable
是正确的:log.debug
记录正确的名称(my_callable
)。
如何制作Pyramid / line_profiler个人资料my_callable
?