Python 進程調用(subprocess)的實現(xiàn)
1. 概述
1.1. subprocess 簡介
在 Python 編程中,subprocess 庫是一個功能強大的工具,它允許我們創(chuàng)建新進程、連接到其輸入/輸出/錯誤管道,并獲取其返回代碼。
1.2. subprocess 庫的功能
subprocess 庫提供了多種功能,以滿足不同的需求。主要功能包括:
- 運行外部命令: 可以執(zhí)行系統(tǒng)命令或調用其他可執(zhí)行文件。
- 交互式進程通信: 啟動外部進程,并通過管道與其進行交互,發(fā)送輸入并獲取輸出。
- 管道和重定向: 創(chuàng)建一個進程的輸出作為另一個進程的輸入,或重定向進程的輸出到文件。
- 進程管理: 監(jiān)控進程狀態(tài),等待進程結束,或終止進程。
2. 應用場景及示例
2.1. 運行外部命令
subprocess.run() 是一個高級接口,用于執(zhí)行外部命令并等待其完成。它返回一個 CompletedProcess 對象,包含執(zhí)行結果的相關信息。
import subprocess
import sys
def run_command_safely(command):
"""安全運行命令,處理Windows和Unix的差異"""
try:
if sys.platform == 'win32':
# Windows 需要特殊處理
result = subprocess.run(command, shell=True, capture_output=True, text=True, encoding='gbk')
else:
# Unix/Linux/Mac
result = subprocess.run(command, capture_output=True, text=True)
return result
except Exception as e:
return subprocess.CompletedProcess(command, 1, '', str(e))
result = run_command_safely(['ls'])
# # 執(zhí)行外部命令并捕獲輸出
print("Return Code:", result.returncode)
print("Standard Output:", result.stdout)
print("Standard Error:", result.stderr)
capture_output=True:捕獲子進程的標準輸出和標準錯誤。
text=True:以文本模式處理輸入/輸出。
2.2. 交互式進程通信
subprocess.Popen() 提供了更靈活的方式來管理子進程,可以通過管道發(fā)送輸入數(shù)據(jù)給子進程,并獲取其輸出。
import subprocess
import sys
def basic_popen(command):
"""基本Popen使用"""
try:
if sys.platform == 'win32':
# 執(zhí)行dir命令
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding='gbk')
else:
result = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
except Exception as e:
return subprocess.CompletedProcess(command, 1, '', str(e))
# 等待進程完成并獲取輸出
stdout, stderr = result.communicate()
# return result
print(f"返回碼: {result.returncode}")
print(f"輸出:\n{stdout}")
if stderr:
print(f"錯誤:\n{stderr}")
basic_popen(['dir'])
[‘dir’]:要執(zhí)行的命令和參數(shù)。
stdin=subprocess.PIPE:通過管道向子進程發(fā)送數(shù)據(jù)。
stdout=subprocess.PIPE:通過管道捕獲子進程的輸出。
process.communicate(input=‘some input data\n’):向子進程發(fā)送輸入數(shù)據(jù)并獲取輸出。
2.3. 管道和重定向
subprocess 庫可以輕松創(chuàng)建管道,將一個進程的輸出重定向到另一個進程的輸入。
import subprocess
# 第一個命令
p1 = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
# 第二個命令,通過管道傳輸數(shù)據(jù)
p2 = subprocess.Popen(['grep', '.py'], stdin=p1.stdout, stdout=subprocess.PIPE)
output = p2.communicate()[0]
print('Python文件列表:')
print(output.decode())
p1.stdout=subprocess.PIPE:將p1的輸出作為管道。
p2.stdin=p1.stdout:將p1的輸出作為p2的輸入。
p2.communicate():獲取p2的最終輸出。
2.4. 進程管理
subprocess.Popen() 還提供了多種方法來管理進程,如poll()、terminate()、kill()等。
import subprocess
import sys
def run_command_safely(command):
"""安全運行命令,處理Windows和Unix的差異"""
try:
if sys.platform == 'win32':
# Windows 需要特殊處理
result = subprocess.Popen(command, shell=True, text=True, encoding='gbk')
else:
# Unix/Linux/Mac
result = subprocess.Popen(command)
# 檢查進程是否仍在運行
print("Process is running:", result.poll() is None)
# 終止進程
result.terminate()
# 等待進程結束
result.wait()
print("Process has terminated")
except Exception as e:
print("Exception", str(e))
return subprocess.CompletedProcess(command, 1, '', str(e))
# 啟動一個子進程
run_command_safely(['sleep', '10'])
process.poll():檢查進程是否仍在運行,返回None表示仍在運行,返回退出碼表示已結束。
process.terminate():請求終止進程。
process.wait():等待進程結束。
3. 與 os.system 和 os.popen 對比
雖然 os.system 和 os.popen 也可以用于執(zhí)行外部命令并與進程進行交互,但它們與 subprocess 相比存在一些限制和不足。
3.1. os.system
os.system 是 Python 中最簡單的執(zhí)行外部命令的方法,但它有幾個顯著的缺點:
無法捕獲輸出: os.system 只能執(zhí)行命令,無法直接捕獲其輸出或錯誤。輸出會直接打印到控制臺。
返回碼有限: 它返回一個8位的字節(jié),表示命令的退出狀態(tài),這可能導致狀態(tài)碼被截斷。
安全性問題: 使用字符串拼接來構建命令可能會導致安全風險,如命令注入。
import os
# 執(zhí)行外部命令
exit_code = os.system('dir')
print("Exit Code:", exit_code)
3.2. os.popen
os.popen 允許我們打開一個與命令的管道,從中讀取輸出。與 os.system 相比,它可以捕獲輸出,但仍然有一些限制:
單向通信: 它只能用于從命令讀取輸出,不能發(fā)送輸入。
資源管理: 需要手動關閉文件對象,否則可能導致資源泄漏。
安全性問題: 同樣存在命令注入的風險。
import os
# 打開一個與命令的管道并讀取輸出
process = os.popen('dir')
output = process.read()
process.close()
print("Output:", output)
3.3. subprocess 的優(yōu)勢
與 os.system 和 os.popen 相比,subprocess 提供了以下優(yōu)勢:
更強大的功能: 支持雙向通信、管道、重定向等高級功能。
更好的資源管理: 可以自動管理文件描述符和進程,減少資源泄漏的風險。
更高的安全性: 通過列表形式傳遞命令和參數(shù),避免了命令注入的風險。
更豐富的返回信息: subprocess.run() 返回一個 CompletedProcess 對象,包含退出碼、輸出和錯誤等信息。
到此這篇關于Python 進程調用(subprocess)的實現(xiàn)的文章就介紹到這了,更多相關Python 進程調用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python NumPy數(shù)組利器之np.zeros函數(shù)詳解與應用實例
在Python的科學計算庫NumPy中,numpy.zeros()是一個非常重要的函數(shù),它用于創(chuàng)建一個指定形狀和數(shù)據(jù)類型的全零數(shù)組,這篇文章主要給大家介紹了關于Python NumPy數(shù)組利器之np.zeros函數(shù)詳解與應用實例的相關資料,需要的朋友可以參考下2024-06-06
Tornado實現(xiàn)多進程/多線程的HTTP服務詳解
這篇文章主要介紹了Tornado實現(xiàn)多進程/多線程的HTTP服務詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值2019-07-07
Python正則表達式re.compile()和re.findall()詳解
re?模塊提供了不少有用的函數(shù),用以匹配字符串,下面這篇文章主要給大家介紹了關于Python正則表達式re.compile()和re.findall()的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07
Python使用Pexpect庫實現(xiàn)自動化與終端交互的任務
Pexpect 是一個 Python 庫,用于自動化與終端交互的任務,它提供了一種簡單的方式來編寫腳本,以便與終端程序進行交互,下面我們就來深入了解一下Pexpect庫的具體使用吧2023-12-12

