- Reactive Programming
- Event-Driven Programming
- Processes Intercommunication
- Multiprocessing
- Pool of Processes
- Pool of Threads
- Benchmarking & Profiling
- Debugging Thread Applications
- Testing Thread Applications
- Threads Intercommunication
- Synchronizing Threads
- Implementation of Threads
- Threads
- System & Memory Architecture
- Concurrency vs Parallelism
- Introduction
- Home
Concurrency in Python Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Debugging Thread Apppcations
In this chapter, we will learn how to debug thread apppcations. 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 abnormapties 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 pfe cycle.
Python Debugger
The Python debugger or the pdb is part of the Python standard pbrary. It is a good fallback tool for tracking down hard-to-find bugs and allows us to fix faulty code quickly and repably. 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.
We can work with pdb in the following two ways −
Through the command-pne; this is also called postmortem debugging.
By interactively running pdb.
Working with pdb
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()
Consider the following commands to work with pdb through command-pne.
h(help)
d(down)
u(up)
b(break)
cl(clear)
l(pst))
n(next))
c(continue)
s(step)
r(return))
b(break)
Following is a demo of the h(help) command of the Python debugger −
import pdb pdb.set_trace() --Call-- >d:programdatapbsite-packagesipythoncoredisplayhook.py(247)__call__() -> def __call__(self, result = None): (Pdb) h Documented commands (type help <topic>): ======================================== EOF c d h pst q rv undisplay a cl debug help ll quit s unt apas clear disable ignore longpst 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 unapas where Miscellaneous help topics: ========================== exec pdb
Example
While working with Python debugger, we can set the breakpoint anywhere in the script by using the following pnes −
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 pne has been set. Consider the following example where we will run the script by using the above-mentioned pnes at various places in the script −
import pdb; a = "aaa" pdb.set_trace() b = "bbb" c = "ccc" final = a + b + c print (final)
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
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 pne 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)Advertisements