代码之家  ›  专栏  ›  技术社区  ›  codeasone

多线程Python代码中Emacs pdb和断点的问题

  •  4
  • codeasone  · 技术社区  · 15 年前

    我正在运行Emacs23.2 python.el 调试一些Python代码 pdb .

    我的代码使用 threading run() pdb公司

    我觉得我可以利用 pdb公司 任何 线程,即使完全多线程调试实际上不受支持。

    我假设错了吗 pdb公司 pdb公司 调用可以在任何线程中中断?如果你不相信我,你自己试试这个最小的例子。

    import threading
    
    class ThreadTest(threading.Thread):
        def __init__(self,):
            threading.Thread.__init__(self)
    
        def run(self):
            print "Type M-x pdb, set a breakpoint here then type c <RET>..."
            print "As you can see it does not break!"
    
    if __name__ == '__main__':
        tt = ThreadTest()
    
        tt.start()
    

    多亏了皮埃尔和他所指的书中的文字,我尝试了包括 pdb.set_trace() 具体如下:

    def run(self):
        import pdb; pdb.set_trace()
        print "Set a breakpoint here then M-x pdb and type c..."
    

    但这只会给你机会 等等,如果它是从控制台执行并直接在Python解释器中运行,那么最关键的是 通过 M-x公司 -至少我的Emacs和 pdb公司 配置。

    如果pdb中的pdb会话包含一个与之相关联的pdb会话,那么如何在pdb中使用pdm-b来处理这个会话的丢失呢?

    2 回复  |  直到 11 年前
        1
  •  1
  •   Matt Harrison    15 年前

    你用默认值吗python.el?我已经放弃了这一点,开始使用python-模式.el. 然后键入 M-x shell ,从提示类型 python myproblem.py set_trace 行。它与pdb集成在一起,可以开箱即用。(它在你的程序中起作用)。

        2
  •  1
  •   Pierre-Antoine LaFayette    15 年前

    http://heather.cs.ucdavis.edu/~matloff/158/PLN/ParProcBook.pdf ,有一节介绍多线程调试。

    3.6.1 Using PDB to Debug Threaded Programs
    Using PDB is a bit more complex when threads are involved. One cannot, for instance, simply do something
    like this:
    pdb.py buggyprog.py
    because the child threads will not inherit the PDB process from the main thread. You can still run PDB in
    the latter, but will not be able to set breakpoints in threads.
    What you can do, though, is invoke PDB from within the function which is run by the thread, by calling
    pdb.set trace() at one or more points within the code:
    import pdb
    pdb.set_trace()
    In essence, those become breakpoints.
    For example, in our program srvr.py in Section 3.1.1, we could add a PDB call at the beginning of the loop
    in serveclient():
    while 1:
    import pdb
    pdb.set_trace()
    # receive letter from client, if it is still connected
    k = c.recv(1)
    if k == ’’: break
    You then run the program directly through the Python interpreter as usual, NOT through PDB, but then the
    program suddenly moves into debugging mode on its own. At that point, one can then step through the code
    using the n or s commands, query the values of variables, etc.
    PDB’s c (“continue”) command still works. Can one still use the b command to set additional breakpoints?
    Yes, but it might be only on a one-time basis, depending on the context. A breakpoint might work only once,
    due to a scope problem. Leaving the scope where we invoked PDB causes removal of the trace object. Thus
    I suggested setting up the trace inside the loop above.