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.
More on Concurrency: threads, processes & asyncio
What is the GIL, and what does it mean for DevOps scripts?IntermediateHow do you use `ThreadPoolExecutor` for parallel API calls?BeginnerThreads vs processes vs asyncio: how do you choose?IntermediateWhat is a race condition, and how do you prevent one in threaded code?IntermediateHow does a producer/consumer pattern with `queue.Queue` work?AdvancedWhat are coroutines, `async def` and `await`?Intermediate