Python debugging helps improve the programming environment

Source: Internet
Author: User

Python debugging needs to be done in many cases. Of course, various situations may occur during the use process. Next, let's take a detailed look at how to debug the Python environment. I hope you will have some gains.

It is reported that the winpdb and Wing IDE debuggers can support such remote debugging, but it seems that the former is much lighter than the latter, but it also requires the wx Python debugging environment, besides, the flexibility and reliability of pdb are incomparable ).

In fact, you only need to make some changes to use pdb to continue debugging the sub-process code in Python. The idea is from this blog: The stdin/out/err of the sub-process is disabled, you can press/dev/stdout again. Of course, this means that in * nix, win will be a little more troublesome. Let's talk about it later.

Pdb supports custom output input files. I will make some changes and use the fifo pipeline (Named Pipe) to redirect the pdb output input. The advantage is that, you can debug both parent and child processes!

 
 
  1. multiproces_debug.py  
  2. #!/usr/bin/python  
  3. import multiprocessing  
  4. import pdb  
  5. def child_process():  
  6. print "Child-Process"  
  7. pdb.Pdb(stdin=open('p_in', 'r+'), stdout=open('p_out', 
    'w+')).set_trace()  
  8. var = "debug me!" 
  9. def main_process():  
  10. print "Parent-Process"  
  11. p = multiprocessing.Process(target = child_process)  
  12. p.start()  
  13. pdb.set_trace()  
  14. var = "debug me!" 
  15. p.join()  
  16. if __name__ == "__main__":  
  17. main_process()  

You only need to input the stdin/stdout file object to the pdb constructor. The output input in the debugging process is naturally oriented to the input file. Here we need two pipeline files p_in and p_out. Before running the script, run the command mkfifo p_in p_out to create them at the same time. This is not complete yet. An external program is required to interact with the pipeline:

 
 
  1. #!/bin/bash  
  2. cat p_out &  
  3. while [[ 1 ]]; do  
  4. read -e cmd  
  5. echo $cmd>p_in  
  6. done  

Very simple bash. Because the read end is blocked when no data is imported to the write end of the fifo pipeline, and vice versa), the cat display is hung in the background. When the debugging program ends, the channel sends an EOF, cat automatically exits.

Experiment started: first run debug_assist.sh on a terminal, but the sequence is irrelevant.) The cursor stops on a new line and then runs multiproces_debug.py on another terminal. It can be seen that the two terminals are simultaneously running (Pdb) can debug the Parent and Child processes at the same time! The preceding section describes how to debug Python.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.