我想知道有沒有一種方法可以在Python中執行固定持續時間的函數?我想它可以使用裝飾器來實現,例如:def terminate_if_runs_too_long(func): def new_func(*args, **kwargs): with <lock which is valid for fixed duration>: return func(*args, **kwargs) return new_func@terminate_if_runs_too_longdef infinite_loop(): '''Imported function, which cannot be modified''' while True: pass但是,我不確定如何實現在固定持續時間內可用的資源/鎖,并且一旦超過使用持續時間就會引發異常。我正在尋找一種可以使用單線程的解決方案。
有沒有辦法在Python中執行固定持續時間的函數?
慕尼黑5688855
2023-12-29 15:08:46