How to Achieve True Parallel Bash Process Execution in Python Without Relying on Threads?

Patricia Arquette
Release: 2024-10-27 05:40:03
Original
161 people have browsed it

How to Achieve True Parallel Bash Process Execution in Python Without Relying on Threads?

How to Spawn Parallel Bash Processes with Python's threading and subprocess Modules?

Original Question:

How can I use Python's threading and subprocess modules to create parallel bash processes? Consecutive execution occurs instead of parallel execution when threads are initiated as described in this Stack Overflow response.

Answer:

Contrary to the original assumption, threads are not necessary for parallel bash subprocess execution. The following techniques provide various options:

Direct Execution (without Threads):

<code class="python">from subprocess import Popen

commands = ['date; ls -l; sleep 1; date', ...]
processes = [Popen(cmd, shell=True) for cmd in commands]</code>
Copy after login

Limited Concurrent Commands Using Threads with multiprocessing.dummy.Pool:

<code class="python">from functools import partial
from multiprocessing.dummy import Pool
from subprocess import call

pool = Pool(2)
for i, returncode in enumerate(pool.imap(partial(call, shell=True), commands)):
    ...</code>
Copy after login

Limit Concurrent Child Processes Without a Process Pool:

<code class="python">from subprocess import Popen
from itertools import islice

max_workers = 2
processes = (Popen(cmd, shell=True) for cmd in commands)
running_processes = list(islice(processes, max_workers))
while running_processes:
    ...</code>
Copy after login

Note for Unix Systems:

On Unix platforms, avoid the busy loop by blocking on os.waitpid(-1, 0) to wait for any child process to exit.

The above is the detailed content of How to Achieve True Parallel Bash Process Execution in Python Without Relying on Threads?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!