Interview Prep/Python for DevOps/Concurrency, async, tests, services & containers

A script with threads ignores Ctrl-C and must be killed with `kill -9`.

AdvancedTroubleshootingConcurrency, async, tests, services & containers
Answer

Likely causes → The main thread is blocked in join() or executor.shutdown(wait=True) and worker threads (non-daemon) keep running; signals are only delivered to the main thread.

Diagnose → py-spy dump --pid <pid> shows the main thread waiting in join.

Fix → Use a threading.Event checked by workers; in the main thread, wait with timeouts (as_completed(..., timeout=...) loops or event.wait(1)), catch KeyboardInterrupt, set the event, and shutdown(cancel_futures=True).

Prevent → Build cooperative cancellation into long-running workers from day one.

All Python for DevOps questions Roadmap: Python for automation

More on Concurrency, async, tests, services & containers