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!
- multiproces_debug.py
- #!/usr/bin/python
- import multiprocessing
- import pdb
- def child_process():
- print "Child-Process"
- pdb.Pdb(stdin=open('p_in', 'r+'), stdout=open('p_out',
'w+')).set_trace()
- var = "debug me!"
- def main_process():
- print "Parent-Process"
- p = multiprocessing.Process(target = child_process)
- p.start()
- pdb.set_trace()
- var = "debug me!"
- p.join()
- if __name__ == "__main__":
- 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:
- #!/bin/bash
- cat p_out &
- while [[ 1 ]]; do
- read -e cmd
- echo $cmd>p_in
- 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.