开发者

PDB won't stop on breakpoint

开发者 https://www.devze.com 2023-04-10 11:28 出处:网络
I\'m quite new with debugging directly with pdb and I am having some issues debugging my Django application. Here is what I\'m doing:

I'm quite new with debugging directly with pdb and I am having some issues debugging my Django application. Here is what I'm doing:

python -m pdb manage.py runserver
(pdb) b core/views.py:22
Breakpoint 2 at /Users/raphaelcruzeiro/Documents/Projects/pdb_test/core/views.py:22
(Pdb) c

However the execution passes directly through the breakpoint. Am I missing some command? The manual d开发者_Python百科oesn't elaborate on setting a breakpoint anymore than this.


I've been through the same problem.

Try something like python -m pdb ./manage.py runserver --nothreading --noreload 127.0.0.1:8080. It solved the issue for me.

It seems that breakpoints with PDB are thread-specific, and the --nothreading and --noreload options are necessary to avoid some forking that may confuse PDB. This is also why set_trace works, as it's called directly inside the thread of interest.


I usually prefer set_trace() in the source itself, that way the dev server will reload when added/removed, and I don't need to stop and start it again. For example:

def get_item(request):
   import pdb; pdb.set_trace()

When the view is accessed, pdb will kick in.


When I've seen this problem in the past, it's usually because someone has set the breakpoint on a line that is not actually connected to a Python statement that is run. For example, blank lines, comment lines, the wrong part of a multi-line statement.


One strange thing I have noticed is that the PDB prompt repeats your previous action when you repeatedly hit enter. Moreover, if you hit enter whilst your program is running, PDB buffers the input and applies it once the prompt appears. In my case, I was running a program using PDB c(ontinue). My program was writing lots of debug info to stdout during initialization, so I hit enter a couple of times to separate the already written output from the output that was going to be written once the breakpoint was triggered. Then, when I triggered the breakpoint via some external action, PDB would stop at the breakpoint but then apply a 'buffered enter' which repeated the c(ontinue) action. Once I stopped hitting enter it all started working normally.

This may sound a bit strange, and I haven't investigated this issue much, but it solved the problem for me. Maybe it helps someone else.


I ran into this issue when writing a neural network in PyTorch. Similar to the accepted answer, the problem was that my DataLoader spun off multiple threads. Removing the num_workers argument allowed me to debug on a single thread.

    train_loader = DataLoader(
        train_dataset,
        batch_size=batch_size,
        num_workers=16, # <-------Remove this argument
        pin_memory=True
    )

If you are running into this issue, a simple fix would be to track down where in your code you are using multiprocessing, and adjust it to run a single thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号