Skip to content

Commit d355dd6

Browse files
author
forozco
committed
add locks
1 parent cfa0387 commit d355dd6

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

pyls/_utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ def debounce(interval_s, keys=None):
1616
"""Debounce calls to this function until interval_s seconds have passed."""
1717
def wrapper(func):
1818
arg_cache = {}
19+
lock = threading.Lock()
1920

2021
@functools.wraps(func)
2122
def cleanup(*args, **kwargs):
2223
input_hash = _hash_input(keys, *args, **kwargs)
23-
del arg_cache[input_hash]
24+
with lock:
25+
del arg_cache[input_hash]
2426
return func(*args, **kwargs)
2527

2628
@functools.wraps(func)
2729
def debounced(*args, **kwargs):
2830
input_hash = _hash_input(keys, *args, **kwargs)
29-
if input_hash in arg_cache:
30-
arg_cache[input_hash].cancel()
31-
timer = threading.Timer(interval_s, cleanup, args, kwargs)
32-
arg_cache[input_hash] = timer
33-
timer.start()
31+
with lock:
32+
if input_hash in arg_cache:
33+
arg_cache[input_hash].cancel()
34+
timer = threading.Timer(interval_s, cleanup, args, kwargs)
35+
arg_cache[input_hash] = timer
36+
timer.start()
3437
return debounced
3538
return wrapper
3639

0 commit comments

Comments
 (0)