Created
September 5, 2019 18:16
-
-
Save santhalakshminarayana/429bde7d17f1ecb9f0c88f4dd975ae42 to your computer and use it in GitHub Desktop.
Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
def process_1(n_sec=0): | |
print(f"Process_1 started at {time.strftime('%H-%M-%S',time.localtime())}" \ | |
f" by {threading.current_thread().name}\n") | |
print(f"Process_1 sleeps for {n_sec} seconds") | |
time.sleep(n_sec) | |
print(f"Process_1 woke up at {time.strftime('%H-%M-%S',time.localtime())}") | |
if __name__=="__main__": | |
print(f"Start time : {time.strftime('%H-%M-%S',time.localtime())}") | |
i=0 | |
n_sec=int(input("Milliseconds to call function?"))*1e-3 | |
times=int(input("No.of times to call?")) | |
while i<times: | |
t1=threading.Timer(n_sec,process_1,args=(0,)) | |
t1.setName("Thread "+str(i+1)) | |
t1.start() | |
t1.join() | |
print() | |
i+=1 | |
''' | |
Start time : 23-42-27 | |
Milliseconds to call function?2000 | |
No.of times to call?5 | |
Process_1 started at 23-42-37 by Thread 1 | |
Process_1 sleeps for 0 seconds | |
Process_1 woke up at 23-42-37 | |
Process_1 started at 23-42-39 by Thread 2 | |
Process_1 sleeps for 0 seconds | |
Process_1 woke up at 23-42-39 | |
Process_1 started at 23-42-41 by Thread 3 | |
Process_1 sleeps for 0 seconds | |
Process_1 woke up at 23-42-41 | |
Process_1 started at 23-42-43 by Thread 4 | |
Process_1 sleeps for 0 seconds | |
Process_1 woke up at 23-42-43 | |
Process_1 started at 23-42-45 by Thread 5 | |
Process_1 sleeps for 0 seconds | |
Process_1 woke up at 23-42-45 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment