Guest User

Untitled

a guest
Jul 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. import time
  2.  
  3. TIME = 10
  4.  
  5.  
  6. def i_will_run_if_takes_more_time(signum, frame):
  7. raise Exception("Time has passed. For now i am just printing!")
  8.  
  9.  
  10. # This function *may* run for an indetermined time...
  11. def i_am_the_main_function():
  12. while 1:
  13. print("main funbction is running...")
  14. time.sleep(1)
  15.  
  16.  
  17. # Register the signal function handler
  18. signal.signal(signal.SIGALRM, i_will_run_if_takes_more_time)
  19.  
  20.  
  21. # Define a timeout for your function
  22. signal.alarm(TIME)
  23.  
  24.  
  25. try:
  26. i_am_the_main_function()
  27. except Exception as e:
  28. print(e)
Add Comment
Please, Sign In to add comment