2

I am trying to use pudb to debug with multiprocessing, but I encounter error as below:

Code:

def worker():
    i = 0
    while i < 10:
        pudb.set_trace()
        i = i + 1
        time.sleep(1)
    
if __name__ == '__main__':
    p1 = multiprocessing.Process(target=worker)
    p1.start()

Error:

File "/usr/local/lib/python2.7/dist-packages/urwid/raw_display.py", line 545, in _getch
    return ord(os.read(self._term_input_file.fileno(), 1))
TypeError: ord() expected a character, but string of length 0 found

Does anyone know about this problem?

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • I'm not familiar with pudb but I think what is happening is that pudb.set_trace() only stops the main process and thus the other processes go on uninterrupted and cause the error. Maybe there is a way to configure pudb to stop all worker processes but if not I would think about just trying to debug it as a single process (turn off multiprocessing). – Kent Shikama Dec 25 '17 at 09:55
  • Thanks kshikama, multiprocessing workers do not have anything attached to stdin, so no interactive debuggers and I have to use remote debug for multiprocess – Alexander Phung Dec 26 '17 at 03:50
  • I have updated workaround – Alexander Phung Dec 26 '17 at 03:57

2 Answers2

2

This works since pudb version 2020.1 using pudb.forked.set_trace().

See also https://documen.tician.de/pudb/starting.html#using-the-debugger-after-forking for a similar example.

Disclosure: I authored this.

0

If you're using an old version of pudb that doesn't support pudb.forked.set_trace(), you can use pudb.remote.set_trace to debug multiprocessing code.

This post has a nice overview of how to do this: https://auro-227.medium.com/use-pudb-to-debug-python-multiprocessing-code-c0c5551d010c

akindofyoga
  • 970
  • 1
  • 9
  • 16