Interview Prep/Python for DevOps/Best practices & rapid-fire commands

Rapid fire: debugging and profiling commands.

AdvancedConceptBest practices & rapid-fire commands
Answer
python -X importtime -c "import cloudaudit.cli" 2>&1 | sort -t'|' -k2 -n | tail   # slow imports
python -m cProfile -s cumtime script.py | head -30
py-spy top --pid 1234                     # sample a running process, no restart
py-spy dump --pid 1234                    # stack of every thread (hung process)
python -X faulthandler script.py          # dump traceback on segfault/hang signal
python -m pdb script.py                   # or breakpoint() in code
PYTHONASYNCIODEBUG=1 python app.py        # slow callbacks, unawaited coroutines

py-spy dump is the fastest way to see where a stuck production script is blocked.

All Python for DevOps questions Roadmap: Python for automation

More on Best practices & rapid-fire commands