I am new to python and I wonder how can we set a specific time for a function to run.
Considering here I have a dataframe with 891 rows called df which has a column name body_text I want to perform a function call summary(to summary the text) through the row of dataframe and store them into a new dataframe called df_summary. Here my goal is to set a function summary to run at most for 2mins it will be good if it can finish before 2mins and move to the next iteration. In case it is not, I want to call the other function name preprocess instead of the function summary that cannot finish within 120s.
Consider we already created the df_summary with 891 rows and a column name body_text. Here is my code + a bit pseudo code as I dont know how to write it exactly:
import time
for i in tqdm(range(0,891)):
# code to detect running time here
df_summary['body_text'][i] = summary(df['body_text'][i])# perform the summary function with at most 120s
# when 120s is over but the summary function cannot finish than perform a preprocessing function instead and move to the next iteration
df_summary['body_text'][i] = preprocess(df['body_text'][i])
Note that if the function summary can run in at most 120s or under that limit time, it wont run the fallback function preprocess and then move to the next iteration.
How can I achieve this? Any help will be much appreciated.