On this page
threading.Timer
Prev: threading.ThreadError | Up: threading
Rel: threading.active_count, threading.Barrier, threading.BoundedSemaphore, threading.BrokenBarrierError
Ref: docs.python.org — threading.Timer
Tags: #public #python3 #stdlib #threading
Import
import threading
from threading import Timer
Signature & Overview
threading.Timer(interval, function, args=None, kwargs=None)
Call a function after a specified number of seconds: t = Timer(30.0, f, args=None, kwargs=None) t.start()
Methods & Attributes
Timer.cancel(self)— Stop the timer if it hasn't finished yet.Timer.daemon(...)— A boolean value indicating whether this thread is a daemon thread.Timer.getName(self)— Return a string used for identification purposes only.Timer.ident(...)— Thread identifier of this thread or None if it has not been started.Timer.isDaemon(self)— Return whether this thread is a daemon.Timer.is_alive(self)— Return whether the thread is alive.Timer.join(self, timeout=None)— Wait until the thread terminates.Timer.name(...)— A string used for identification purposes only.Timer.native_id(...)— Native integral thread ID of this thread, or None if it has not been started.Timer.run(self)—Timer.runmethod/property.Timer.setDaemon(self, daemonic)— Set whether this thread is a daemon.Timer.setName(self, name)— Set the name string for this thread.Timer.start(self)— Start the thread's activity.
Usage
import threading
fired = threading.Event()
t = threading.Timer(0.01, fired.set)
t.start()
assert fired.wait(timeout=2) is True
t.join()
Gotchas
- Cancel before it fires with
t.cancel(): Once the callback has already started executing,cancel()has no effect. - Spawns a new OS thread per timer: Don't create thousands of
Timerobjects—useschedorasyncioinstead.
REPL Inspect
from threading import Timer
help(Timer)
Linked from
Related notes
- tuts/python3/stdlib_module/threading/ThreadError Linked here; links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/threading/active_count Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/Barrier Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/BoundedSemaphore Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/BrokenBarrierError Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/Condition shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/current_thread shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/enumerate shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/Event shared tags: #stdlib, #threading