3

我无法使用 line_profiler 分析 Pyramid 视图。由于 Pyramid@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(...):

现在,分析运行.. 除了似乎 Pyramid 做了一些黑魔法,而不是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

4

0 回答 0