Concurrency In Python 简明教程
Debugging Thread Applications
在本章中,我们将学习如何调试线程应用程序。我们还将了解调试的重要性。
In this chapter, we will learn how to debug thread applications. We will also learn the importance of debugging.
What is Debugging?
在计算机编程中,调试是查找和删除计算机程序中的错误、错误和异常的过程。这个过程在编写代码时就开始,并在作为代码与其他编程单元组合形成软件产品的过程中持续各个阶段。调试是软件测试过程的一部分,并且是整个软件开发生命周期中不可或缺的一部分。
In computer programming, debugging is the process of finding and removing the bugs, errors and abnormalities from computer program. This process starts as soon as the code is written and continues in successive stages as code is combined with other units of programming to form a software product. Debugging is part of the software testing process and is an integral part of the entire software development life cycle.
Python Debugger
Python 调试器或 pdb 是 Python 标准库的一部分。它是一个可靠的后备工具,用于追踪难以发现的错误,并允许我们快速可靠地修复有缺陷的代码。以下是最重要的 pdp 调试器的两个任务 −
The Python debugger or the pdb is part of the Python standard library. It is a good fallback tool for tracking down hard-to-find bugs and allows us to fix faulty code quickly and reliably. Followings are the two most important tasks of the pdp debugger −
-
It allows us to check the values of variables at runtime.
-
We can step through the code and set breakpoints also.
我们可以通过以下两种方式使用 pdb :
We can work with pdb in the following two ways −
-
Through the command-line; this is also called postmortem debugging.
-
By interactively running pdb.
Working with pdb
要使用 Python 调试器工作,我们需要在希望进入调试器的位置使用以下代码:
For working with the Python debugger, we need to use the following code at the location where we want to break into the debugger −
import pdb;
pdb.set_trace()
考虑使用以下命令通过命令行使用 pdb。
Consider the following commands to work with pdb through command-line.
-
h(help)
-
d(down)
-
u(up)
-
b(break)
-
cl(clear)
-
l(list))
-
n(next))
-
c(continue)
-
s(step)
-
r(return))
-
b(break)
以下是 Python 调试器中 h(help) 命令的演示:
Following is a demo of the h(help) command of the Python debugger −
import pdb
pdb.set_trace()
--Call--
>d:\programdata\lib\site-packages\ipython\core\displayhook.py(247)__call__()
-> def __call__(self, result = None):
(Pdb) h
Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where
Miscellaneous help topics:
==========================
exec pdb
Example
使用 Python 调试器时,我们可以通过使用以下行在脚本中的任何位置设置断点:
While working with Python debugger, we can set the breakpoint anywhere in the script by using the following lines −
import pdb;
pdb.set_trace()
设置断点后,我们可以正常运行脚本。脚本将执行到一个特定的点;直到设置了一个行的位置。考虑以下示例,我们将在脚本的不同位置使用上述行来运行它:
After setting the breakpoint, we can run the script normally. The script will execute until a certain point; until where a line has been set. Consider the following example where we will run the script by using the above-mentioned lines at various places in the script −
import pdb;
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print (final)
当运行上述脚本时,它将执行程序直到 a =“aaa”,我们可以通过以下输出进行检查。
When the above script is run, it will execute the program till a = “aaa”, we can check this in the following output.
Output
--Return--
> <ipython-input-7-8a7d1b5cc854>(3)<module>()->None
-> pdb.set_trace()
(Pdb) p a
'aaa'
(Pdb) p b
*** NameError: name 'b' is not defined
(Pdb) p c
*** NameError: name 'c' is not defined
在 pdb 中使用命令‘p(print) ’后,此脚本仅打印“aaa”。其次是错误,因为我们已将断点设置为 a =“aaa”。
After using the command ‘p(print)’ in pdb, this script is only printing ‘aaa’. This is followed by an error because we have set the breakpoint till a = "aaa".
同样,我们可以通过更改断点并查看输出的差异来运行脚本:
Similarly, we can run the script by changing the breakpoints and see the difference in the output −
import pdb
a = "aaa"
b = "bbb"
c = "ccc"
pdb.set_trace()
final = a + b + c
print (final)
Output
--Return--
> <ipython-input-9-a59ef5caf723>(5)<module>()->None
-> pdb.set_trace()
(Pdb) p a
'aaa'
(Pdb) p b
'bbb'
(Pdb) p c
'ccc'
(Pdb) p final
*** NameError: name 'final' is not defined
(Pdb) exit
在以下脚本中,我们在程序的最后一行中设置断点:
In the following script, we are setting the breakpoint in the last line of the program −
import pdb
a = "aaa"
b = "bbb"
c = "ccc"
final = a + b + c
pdb.set_trace()
print (final)
输出如下 −
The output is as follows −
--Return--
> <ipython-input-11-8019b029997d>(6)<module>()->None
-> pdb.set_trace()
(Pdb) p a
'aaa'
(Pdb) p b
'bbb'
(Pdb) p c
'ccc'
(Pdb) p final
'aaabbbccc'
(Pdb)