import threading
import time
def task(id: int):
time.sleep(1)
print('task done', id)
thread_list = []
start_time = time.time()
for i in range(3):
t = threading.Thread(target=task, args=[i])
t.start()
thread_list.append(t)
for t in thread_list:
t.join()
end_time = time.time()
print('time', end_time - start_time)
import threading
from typing import Callable, Optional
def debounce(wait: float):
def decorator(fn: Callable):
timer: Optional[threading.Timer] = None
def debounced(*args, **kwargs):
nonlocal timer
def call_fn():
fn(*args, **kwargs)
if timer:
timer.cancel()
timer = threading.Timer(wait, call_fn)
timer.start()
return debounced
return decorator
@debounce(1)
def foo(num):
print('foo', num)
for i in range(100):
foo(i)