4

I found this snippet of code somewhere:

t = Timer(10.0, hello)
t.start()

Where 10.0 is the time in seconds for when the timer is supposed to execute, and hello is the method that will run when the time parameter is met. However, I can't find the module this function belongs to. Any help?

4 Answers4

4

This depends on your code logic. But from the code supplied, most probably it is Timer from threading module, so you just have to add this at the top of your code

from threading import Timer

Documentation is here: threading.Timer

Ammar Hasan
  • 2,436
  • 16
  • 22
3

Even though this is probably not the case here given the context, it is also possible that the Timer is from the timeit module which allows to run tests on the quickness of execution.

That is, from timeit import Timer

https://docs.python.org/3/library/timeit.html

MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • 1
    ```from timeit import Timer``` worked exactly for my needs. Thank you @MadPhysicist ```from threading import Timer``` came back with error: ```AttributeError: 'Timer' object has no attribute 'timeit'``` – bananaforscale Jan 20 '18 at 01:29
2

Most likely, it is the Timer class contained in the threading module:

>>> import threading
>>> threading.Timer
<function Timer at 0x01B8ECF0>
>>>
0

Timer() is a class in the threading module:

This class represents an action that should be run only after a certain amount of time has passed — a timer. Timer is a subclass of Thread and as such also functions as an example of creating custom threads.

Timers are started, as with threads, by calling their start() method. The timer can be stopped (before its action has begun) by calling the cancel() method. The interval the timer will wait before executing its action may not be exactly the same as the interval specified by the user.

Community
  • 1
  • 1
MattDMo
  • 100,794
  • 21
  • 241
  • 231