Python 中 Bash 子程序的平行處理
順序運行子程序可能會影響應用程式的效能。若要同時執行多個 bash 指令,您可以利用 Python 中的執行緒和子程序模組。
直接使用子程序
雖然執行緒對於平行處理似乎是必要的,但事實並非如此必需的。您可以使用subprocess.Popen 函數並行啟動進程:
<code class="python">from subprocess import Popen commands = [ 'date; ls -l; sleep 1; date', 'date; sleep 5; date', 'date; df -h; sleep 3; date', 'date; hostname; sleep 2; date', 'date; uname -a; date', ] # Run commands in parallel processes = [Popen(cmd, shell=True) for cmd in commands] # Perform other tasks while processes run # Wait for completion for p in processes: p.wait()</code>
限制並發子進程
要限制並發進程的數量,請考慮使用多處理.dummy>
要限制並發進程的數量,請考慮使用多處理.dummy .Pool 模組,使用執行緒模擬多重處理:<code class="python">from functools import partial from multiprocessing.dummy import Pool from subprocess import call pool = Pool(2) # Limit to two concurrent commands # Iterate over commands and return codes for i, returncode in enumerate(pool.imap(partial(call, shell=True), commands)): if returncode != 0: print("%d command failed: %d" % (i, returncode))</code>
無池限制
您也可以在沒有執行緒池的情況下限制並發:<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)) # Start initial processes while running_processes: for i, process in enumerate(running_processes): if process.poll() is not None: # Process has finished running_processes[i] = next(processes, None) # Start new process if running_processes[i] is None: # No new processes del running_processes[i] break</code>
以上是如何在 Python 中同時執行多個 Bash 指令?的詳細內容。更多資訊請關注PHP中文網其他相關文章!