'Cannot use pudb for multiprocessing

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2