跟踪脚本的执行情况? [英] Tracing the execution of scripts?

查看:101
本文介绍了跟踪脚本的执行情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我似乎对我正在寻找的东西感到茫然,而且即使有可能,我也不会确定。我找到了''pdb''

调试器,但我想知道是否有某些内容可以跟踪或

记录多模块Python的行执行顺序程序。我是b $ b跟踪我之前提到的
问题有点问题

http://groups.google.com/group/comp....59fc888b365be)

我希望看到这些陈述的执行情况将会给我带来一些启示。
调试器不工作,因为我有一个定时器设置为每隔20ms触发
来检查来自
$ b的传入网络流量$ b服务器,并且该计时器启动时使用

调试器似乎无法获得任何有用的数据。


基本上,有什么东西可以记录每行Python代码

按执行顺序执行到文本文件,以便我可以看到

什么是(或不是)发生的我在期待?我知道

服务器正在正确发送和接收,因为我可以使用常规的telnet客户端连接到它b / b
并且它可以工作 - 但同时,

我能想到的一切都可以检查我的程序的功能检查

就好了(例如,它报告说它成功发送了我的内容

输入到服务器,它说服务器没有发送

任何东西都要从套接字读取。


如果它使任何区别,我使用wxWidgets的'Python绑定来获取UI代码(和计时器),以及使用Python的套接字库来获取

网络的东西。


- Mike

Alright, I seem to be at a loss for what I am looking for, and I am not
even really all that sure if it is possible or not. I found the ''pdb''
debugger, but I was wondering if there was something that would trace or
log the order of line execution for a multi-module Python program. I am
having a little bit of a problem tracking down a problem that I
mentioned earlier
(http://groups.google.com/group/comp....59fc888b365be),
and I am hoping that maybe seeing how the statements are executed will
shed some light on the entire thing for me.

The debugger isn''t working, though, because I have a timer set up to
fire off every 20ms to check for incoming network traffic from the
server, and that timer firing off makes stepping through with the
debugger seemingly impossible to get any useful data.

Basically, is there something that will log every line of Python code
executed, in its order of execution, to a text file so that I can see
what is (or isn''t) happening that I am expecting? I know that the
server is sending and receiving properly, because I can connect to it
with a regular telnet client and it works -- but at the same time,
everything I can think of to check my program''s functionality checks out
just fine (e.g., it reports that it is successfully sending what I am
typing to the server, and it says that the server is not sending
anything back to be read from the socket).

If it makes any difference, I am using wxWidgets'' Python bindings for
the UI code (and the timer), and using Python''s socket library for the
network stuff.

-- Mike

推荐答案

Jean-Paul Calderone< ex ***** @ divmod .comwrites:
Jean-Paul Calderone <ex*****@divmod.comwrites:

1)为您的代码编写单元测试。继续编写单元测试,直到

你有一些_don't pass_。然后修复你的代码,以便他们

。当你进行进一步的开发时,首先编写测试,然后

实现使它们通过的代码。
1) Write unit tests for your code. Keep writing unit tests until
you have some that _don''t pass_. Then fix your code so that they
do. When you do further development, write the tests first, then
implement the code that makes them pass.



听到。


但是,请注意,尝试将单元测试应用于代码

没有考虑到测试设计可能很快就会发现一个可怜的设计。 [0]如果你不能轻易地独立测试代码片段,

你可能还没有写出那些松散耦合的片段和

明确定义。


道德?编写单元测试*以及*功能代码将

导致设计松散耦合,并且可能更好地定义了
。另外,希望很容易测试:-)

[0]我不知道这是否属于OP的情况。这是一个非常好的b / b
因为第一次被建议的人而产生的常见症状

为他们的代码引入了测试。


-

\如果你遇到火灾,尽量避免看到自己在|

` \镜子,因为我敢打赌''什么真的会让你陷入恐慌。#$ $
_o__)恐慌。 - Jack Handey |
Ben Finney

Hear hear.

Be advised, though, that attempting to apply unit tests to code that
wasn''t designed with testing in mind may very quickly reveal a poor
design. [0] If you can''t easily test pieces of the code independently,
you probably haven''t written those pieces to be loosely coupled and
well-defined.

The moral? Writing unit tests *along with* the functional code will
result in a design that is loosely coupled, and probably
better-defined. Also, hopefully, easy to test :-)
[0] I have no idea whether this is the case for the OP. It''s a very
common symptom that arises from people who are first advised to
introduce tests to their code, though.

--
\ "If you ever catch on fire, try to avoid seeing yourself in the |
`\ mirror, because I bet that''s what REALLY throws you into a |
_o__) panic." -- Jack Handey |
Ben Finney


" Michael B. Trausch" <" mike
"Michael B. Trausch" <"mike


#at ^& nospam!%trauschus"写道:
#at^&nospam!%trauschus"wrote:

基本上,有什么东西吗会记录每行Python代码

按执行顺序执行到文本文件,以便我可以看到

什么是(或不是)发生的事情我期待的是什么?
Basically, is there something that will log every line of Python code
executed, in its order of execution, to a text file so that I can see
what is (or isn''t) happening that I am expecting?



Python本身可以为你做这件事。一个__VERY__简单的方法:


| def myCallTrace(frame,event,arg):

| tracefile.write(''localTrace \ n frame:''+ str(frame)+''\ n event:

|''+ str(event)+''\ n arg :''+ str(arg)+''\ n'')

| tracefile.write(''''+ str(inspect.getframeinfo(frame))+''\ n'')

|

| def myTrace(frame, event,arg):

| tracefile.write(''globalTrace \ n frame:''+ str(frame)+''\ n event:

|''+ str(event)+''\ n arg :''+ str(arg)+''\ n'')

| if event ==''call'':

|返回myCallTrace

|

|导入检查

| import sys

|

| sys.settrace(myTrace)

| tracefile = file(''trace.txt'',''w'')


在你的插入程序,你会得到跟踪文件trace.txt中执行的每行Python代码

的痕迹。您必须阅读

模块检查和sys.settrace()的文档才能理解,会发生什么以及

意味着什么。另外,如果它应该与线程一起使用,你必须小心

每个线程都有自己的输出文件。但是作为第一步获得执行的b
,这应该是这样的。


HTH,问候

Stephan

Python itself can do this for you. A __VERY__ simple approach:

|def myCallTrace (frame, event, arg):
| tracefile.write(''localTrace\n frame: ''+str(frame)+''\n event:
|''+str(event)+''\n arg: ''+str(arg)+''\n'')
| tracefile.write('' ''+str(inspect.getframeinfo(frame))+''\n'')
|
|def myTrace (frame, event, arg):
| tracefile.write(''globalTrace\n frame: ''+str(frame)+''\n event:
|''+str(event)+''\n arg: ''+str(arg)+''\n'')
| if event == ''call'':
| return myCallTrace
|
|import inspect
|import sys
|
|sys.settrace(myTrace)
|tracefile = file(''trace.txt'', ''w'')

Insert this in you program and you get a trace of every line of Python-Code
executed in the file trace.txt. You must read the documentation of the
module inspect and of sys.settrace() to understand, what happens and what
it means. Additionally, if it should work with threads, you must take care
that every thread gets its own output file. But as a first step to get a
trace of execution, this should do it.

HTH, Regards
Stephan


这篇关于跟踪脚本的执行情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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