Advertisement
makispaiktis

DCP10 - Calling function after n milliseconds

Sep 5th, 2020 (edited)
1,571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. # Implement a job scheduler, which takes in a function f and an integer n
  2. # and calls f after n milliseconds
  3.  
  4. from timeit import default_timer as timer
  5. def f(n):
  6.     if n < 0 or n != int(n):
  7.         return "Error using the function 'f'"
  8.     elif n == 0:
  9.         return "Ending time: " + str(timer())
  10.     else:
  11.         start = timer()
  12.         print("Starting time = " + str(start))
  13.         while 1000 * (timer() - start) < n:
  14.             continue
  15.         return f(0)
  16.  
  17. # MAIN FUNCTION
  18. millis = [10, 50, 500, 1000, 3000, 5000]
  19. for milli in millis:
  20.     print("**** milliseconds = " + str(milli) + " ****")
  21.     print(f(milli))
  22.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement