How to do it...

Let's perform the following steps:

  1. 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()
  1. 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()
  1. 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
  1. Then, you can switch to debug the sample code remotely by making the following telnet connection:
telnet localhost 4444
  1. The following window should open:

  1. In the sample code, note the arrow in line 7. The code is not running, it is just waiting for an instruction to execute:

  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.172.68