Skip to content

Commit

Permalink
Add def multiprocessing_breakpoint function
Browse files Browse the repository at this point in the history
Using the builtin `breakpoint` function does not work with
multiprocessing, as the Process forks has different STDIN so the pdb can
not attach to it. Use this `multiprocessing_breakpoint` function
instead, when you need a breakpoint for debugging.
  • Loading branch information
kukovecz committed Jan 25, 2022
1 parent 45960a5 commit 07b1789
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions unblob/logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import pdb
import sys
from os import getpid
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -79,3 +81,18 @@ def configure_logger(verbose: bool, extract_root: Path):
wrapper_class=structlog.make_filtering_bound_logger(log_level),
processors=processors,
)


class _MultiprocessingPdb(pdb.Pdb):
def interaction(self, *args, **kwargs):
_stdin = sys.stdin
try:
sys.stdin = open("/dev/stdin")
pdb.Pdb.interaction(self, *args, **kwargs)
finally:
sys.stdin = _stdin


def multiprocessing_breakpoint():
"""Call this in Process forks instead of the builtin `breakpoint` function for debugging with PDB."""
return _MultiprocessingPdb().set_trace()

0 comments on commit 07b1789

Please sign in to comment.