In parallel processing scenarios, sequential execution can be a bottleneck. To circumvent this issue, explore how to execute multiple 'cat | zgrep' commands simultaneously in Python while retaining the individual output for further processing.
For concurrent subprocess execution without resorting to multiprocessing or threading, consider the following approach:
<code class="python">#!/usr/bin/env python from subprocess import Popen # Initialize processes processes = [Popen("echo {i:d}; sleep 2; echo {i:d}".format(i=i), shell=True) for i in range(5)] # Gather execution statuses exitcodes = [p.wait() for p in processes]</code>
This code launches five shell commands in parallel without requiring '&' or explicit '.wait()' calls.
For concurrent subprocess output collection, threads can be employed:
<code class="python">#!/usr/bin/env python from multiprocessing.dummy import Pool from subprocess import Popen, PIPE, STDOUT # Create processes processes = [Popen("echo {i:d}; sleep 2; echo {i:d}".format(i=i), shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) for i in range(5)] # Collect output def get_lines(process): return process.communicate()[0].splitlines() outputs = Pool(len(processes)).map(get_lines, processes)</code>
This code gathers subprocess output in parallel using a thread pool.
In Python 3.8 , asyncio can be utilized for concurrent output collection in a single thread:
<code class="python">#!/usr/bin/env python3 import asyncio import sys from subprocess import PIPE, STDOUT async def get_lines(shell_command): p = await asyncio.create_subprocess_shell( shell_command, stdin=PIPE, stdout=PIPE, stderr=STDOUT ) return (await p.communicate())[0].splitlines() async def main(): # Concurrent command execution coros = [ get_lines( f'"{sys.executable}" -c "print({i:d}); import time; time.sleep({i:d})"' ) for i in range(5) ] print(await asyncio.gather(*coros)) if __name__ == "__main__": asyncio.run(main())</code>
This code executes the subprocesses and collects their output asynchronously, eliminating the need for multiprocessing or threading.
The above is the detailed content of How can you achieve concurrent execution of 'cat | zgrep' commands in Python while efficiently managing individual output for further processing?. For more information, please follow other related articles on the PHP Chinese website!