Let's perform the following steps:
- Consider the following sample code:
import threading
def my_func(thread_number):
return print('my_func called by thread N°
{}'.format(thread_number))
def main():
threads = []
for i in range(10):
t = threading.Thread(target=my_func, args=(i,))
threads.append(t)
t.start()
t.join()
if __name__ == "__main__":
main()
- To use rpdb, you need to insert the following lines of code (just after the import threading statement). In fact, these three lines of code enable the use of rpdb via a remote client on port 4444 with an IP address of 127.0.0.1:
import rpdb
debugger = rpdb.Rpdb(port=4444)
rpdb.Rpdb().set_trace()
- If you run the sample code after inserting these three lines of code that enable the use of rpdb, then you should see the following message on Python Command Prompt:
pdb is running on 127.0.0.1:4444
- Then, you can switch to debug the sample code remotely by making the following telnet connection:
telnet localhost 4444
- The following window should open:
- In the sample code, note the arrow in line 7. The code is not running, it is just waiting for an instruction to execute:
- For example, here, we execute the code and type the next statement repeatedly:
(Pdb) next
> c:usersgiancarlodesktoppython parallel programming cookbook 2nd editionpython parallel programming new bookchapter_x- code debugging pdb_code_example.py(10)<module>()
-> def main():
(Pdb) next
> c:usersgiancarlodesktoppython parallel programming cookbook 2nd editionpython parallel programming new bookchapter_x- code debugging pdb_code_example.py(18)<module>()
-> if __name__ == "__main__":
(Pdb) next
> c:usersgiancarlodesktoppython parallel programming cookbook 2nd editionpython parallel programming new bookchapter_x- code debugging pdb_code_example.py(20)<module>()
-> main()
(Pdb) next
my_func called by thread N 0
my_func called by thread N 1
my_func called by thread N 2
my_func called by thread N 3
my_func called by thread N 4
my_func called by thread N 5
my_func called by thread N 6
my_func called by thread N 7
my_func called by thread N 8
my_func called by thread N 9
--Return--
> c:usersgiancarlodesktoppython parallel programming cookbook 2nd editionpython parallel programming new bookchapter_x- code debugging pdb_code_example.py(20)<module>()->None
-> main()
(Pdb)
Once the program is finished, you can still run a new debug section. Now, let's see how rpdp works in the next section.