Cannot use pudb for multiprocessing

633 views Asked by At

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?

2

There are 2 answers

0
Jonathan Striebel On

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
skirodge On

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