Interview Prep/Python for DevOps/Concurrency: threads, processes & asyncio

When is `subprocess` itself a form of parallelism?

AdvancedConceptConcurrency: threads, processes & asyncio
Answer

External commands run as separate processes, so you can start several with Popen and wait for all, or run subprocess.run calls from a thread pool. The GIL is irrelevant because the work happens in the child processes.

with ThreadPoolExecutor(max_workers=4) as pool:
    results = list(pool.map(lambda d: subprocess.run(["terraform", "-chdir=" + d, "plan", "-no-color"],
                                                      capture_output=True, text=True), dirs))

Bound the parallelism: four terraform plan processes each hitting provider APIs can trigger throttling or exhaust memory on a CI runner.

All Python for DevOps questions Roadmap: Python for automation

More on Concurrency: threads, processes & asyncio