簡體   English   中英

使用Python的子流程模塊,有沒有辦法在調用被卡住時跳過執行?

[英]Using Python's subprocess module, is there a way to skip an execution when the call gets stuck?

作為較大程序的一部分,我正在使用

一個subprocess.call('some external program', shell=True)

(這是一個舊的C程序,我的編譯版本僅位於網絡服務器上,因此我不能僅獲取C源代碼並將其與Python進行接口)

臨時生成一些數據,我再次讀入Python。 問題在於通過子進程調用的外部程序有時會卡在某些輸入文件上。 有沒有一種方法可以跳過subprocess.call() ,例如,如果外殼在某個任意時間限制后沒有響應,該方法是什么?

E.g., 

# for data in directory:
    # do sth.
    subprocess.call('call prog.', shell=True) # skip if takes longer than 5 min
    # analyze data

從Python 3.3開始,您可以以秒為單位設置timeout參數

try:
    return_code = subprocess.call('call prog.', shell=True, timeout=10)
except TimeoutExpired:
    # handle timeout exception here which gets throw after
    # 10 seconds in this example

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM