0

I am typing this code in Google Colabs

def mytimer(): 
     print("Python Program\n") 
my_timer = threading.Timer(0.8, mytimer) 
my_timer.start() 
print("Bye\n") 

In some cases I get the output as

Bye

At other times, I get

Bye

Python Program

Why is this difference occuring? Should I add any other line of code? Or any criteria I should be careful about?

Rensi Sam
  • 118
  • 13

1 Answers1

0

with 0.1You are missing few things here enter image description here

import logging
import threading
import time
def mytimer(): 
     print("Python Program\n") 
my_timer = threading.Timer(0.2, mytimer) # change it to 0.8 will not print
my_timer.start() 
print("Bye\n") 

threading.Timer - As per docs Call a function after a specified number of seconds when it is 0.2 it will call that function and it prints both when it is 0.8 it is not calling that function so it is not calling, This has nothing to do with colab you can refer this

nithin
  • 753
  • 3
  • 7
  • 21
  • i didn't get the effect of timing difference – Rensi Sam Jan 02 '20 at 10:56
  • i didn't get why it is getting printed when the time is 0.2 and not getting printed when it is 0.8?could any one explain – Rensi Sam Jan 08 '20 at 05:55
  • threading.Timer(interval, function, args = None, kwargs = None) , It calls the function after 0.2 Seconds is passed in your case it calls def mytimer(): print("Python Program\n"). However when it is 0.8 seconds that much time is not passed so it doesn't call that function just prints the next statement print("Bye\n") . Hope this helps for better understanding use the link in earlier answer and read python docs https://docs.python.org/3/library/threading.html – nithin Jan 08 '20 at 08:26