The simplest way to remotely debug a Python multi-process subroutine

Source: Internet
Author: User

Python 2.6 New multiprocessing, that is, multi-process, to the sub-process code debugging a bit difficult, such as Python's own PDB if you start directly in the child Process code will throw a bunch of exceptions, because the child process stdin/out/ Err and other files are closed and PDB cannot be called. Reportedly winpdb, Wing IDE Debugger can support such remote debugging, but seems to be too heavy (well the former is much lighter than the latter, but the same to Wxpython environment, and then the PDB flexible and reliable they are difficult to compare).

In fact, just a little change can use the PDB to continue to debug the code of the sub-process, the idea from this blog: The Stdin/out/err of the child process is closed, you can re-press the name of/dev/stdout to open to use. Of course, this refers to *nix under, win under the trouble, and later.

The PDB supports custom output input files, and I make a slight change, using the FIFO pipe (Named pipe) to complete the PDB output input redirection, so the benefit is that the parent-child process can be debugged at the same time!

multiproces_debug.py

#!/usr/bin/python Import multiprocessingimport 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 ()

Just pass in the Stdin/stdout file object to the PDB's construction parameters, and the output input of the debugging process will naturally be in the direction of the incoming file. Here you need two pipe files p_in, P_out, before running the script, using the command Mkfifo p_in P_out is also established. This is not done yet and an external program is required to interact with the pipeline:

debug_cmd.sh

#!/bin/bash cat P_out &while [[1]]; Do    read-e cmd    echo $cmd >p_indone

Very simple bash. Because the FIFO pipeline is blocked on the write side (and vice versa), the display of the cat hangs in the background, and when the debugged program finishes, the pipeline exits Eof,cat automatically.

Experiment start: First run debug_cmd.sh in a terminal (in fact, the order is irrelevant), its cursor stops in a new line, and then run multiproces_debug.py in another terminal, visible to two terminals at the same time (PDB) indicator, you can simultaneously debug the parent-child process!

It is not so convenient to use pipelines under Windows, because there is no pipe file support for the entity, consider using the socket's class file object to pass to the PDB. But the Python code to write more, and to be used as an interactive program, but still not much code, you can write a module dedicated to remote debugging, import is used. It is not implemented, and the code will be put out later.

Update: Dedicated debug module See "PDB Remote debugging Python multi-Process subroutine"

The simplest way to remotely debug a Python multi-process subroutine

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.