Skip to content

decorators.thread

sikvelsigma edited this page Oct 22, 2022 · 7 revisions

Summery

  • decorator repeat_timer

Sets a separate thread that will call a function every set amount of seconds. Returns a Queue-like object that will let you retrieve accumulated results

  • decorator cash_timer

Calls a function in a separate thread every x seconds and stores the result

Contains

repeat_timer

from pyuseful.decorators.thread import repeat_timer

import time

@repeat_timer(timer=1, get_nones=False, get_false=False)
def fun(msg):
    print(msg)
    return 1

a = fun("hi")
time.sleep(2)
print(a.get())  # will print [1, 1]
time.sleep(2)
print(a.get())  # will print [1, 1]
a.stop()

cash_timer

from pyuseful.decorators.thread import cash_timer

import time

# Calls current_time() every 1 sec and stores the result
@cash_timer(timer=1, timeout=10)
def current_time():
    return time.time()

a = current_time()
print(a.get())
time.sleep(2)
print(a.get())
a.stop()

Clone this wiki locally