python:如何跟踪大型项目中的函数执行顺序 [英] python: How to trace function execution order in large project

查看:91
本文介绍了python:如何跟踪大型项目中的函数执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在scrapy框架中跟踪功能/类执行命令.默认项目中有多个* .py文件,我想知道哪个py文件和类已按顺序执行.在每个类和每个函数中都放置记录器行听起来很愚蠢.如何形象地显示此顺序?

cprofile主要用于测量总时间.我还可以可视化一个模块内的执行顺序,这是常见的问题,但是很难可视化多个模块.

就跟踪包而言,我没有找到合适的示例来处理诸如scrapy或django之类的大型项目.跟踪用法教程仅涉及一个python文件.

我想在一个大型项目中的多个模块(例如scrapy)中跟踪多个* .py文件,而不是一个模块.

我知道像pdb这样的调试工具,但是我发现在整个项目中放置断点很麻烦.更重要的是,总结执行顺序并不容易.

最后我通过使用Hunter来解决,它比内置跟踪模块要好.跟踪模块不提供include_dir属性.

对于那些对如何追踪所有刮擦痕迹充满好奇的人.

  $ PYTHONHUNTER ='Q(module_startswith = ["scrapy","your_project"])'抓取清单 


对于django,跟踪rest_framework的执行代码并将其保存到test.log,例如:

  $ PYTHONHUNTER ='Q(module_startswith = ["rest_framework","your_project"]),action = CallPrinter(stream = open("test.log","w"))'python manage.py运行服务器--noreload --nothreading 

解决方案

跟踪

I want to trace function/class executive order in scrapy framework. There are multiple *.py files across the default project, and I want to know which py file and class has been executed in order. It sound silly to put logger line in every class and function. How to visualize this order?

cprofile is mainly used for measuring total time. I could also visualize the execution order inside one module, which is common question, but visualizing multiple modules are difficult.

In terms of trace package, I did not find appropriate examples to work with large project like scrapy or django. Trace usage tutorial is about a single python file.

I want to trace multiple *.py files in multiple modules in a large project, eg scrapy, instead of just one module.

I am aware of debug tools like pdb, but I find it cumbersome to put break point across the whole project. More importantly, it is not easy to summarize the execution order.

Finally I solved by using Hunter, which is better than build-in trace module. Trace module did not offer include_dir attribute.

For those who are curiosity about how to trace all lines of scrapy.

$PYTHONHUNTER='Q(module_startswith=["scrapy", "your_project"])' scrapy list 


In terms of django, tracing execution codes of rest_framework and save to test.log, for example:

$PYTHONHUNTER='Q(module_startswith=["rest_framework", "your_project"]), action=CallPrinter(stream=open("test.log", "w"))' python manage.py runserver --noreload --nothreading

解决方案

trace

The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed during a program run. It can be used in another program or from the command line.

python -m trace --count -C . somefile.py ...

The above will execute somefile.py and generate annotated listings of all Python modules imported during the execution into the current directory.

PDB

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.

Most Common Used Command:

w(here)

  • Print a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands.

d(own)

  • Move the current frame one level down in the stack trace (to a newer frame).

u(p)

  • Move the current frame one level up in the stack trace (to an older frame).

You can also check this question Python debugging tips

Coverage

Coverage.py measures code coverage, typically during test execution. It uses the code analysis tools and tracing hooks provided in the Python standard library to determine which lines are executable, and which have been executed.

Hunter

Hunter is a flexible code tracing toolkit, not for measuring coverage, but for debugging, logging, inspection and other nefarious purposes.

The default action is to just print the code being executed. Example:

import hunter
hunter.trace(module='posixpath')

import os
os.path.join('a', 'b')

Result in terminal:

这篇关于python:如何跟踪大型项目中的函数执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆