def threadsafe(function):
 lock = threading.RLock()
 def decorator(*args, **kwargs):
  with lock:
   v = function(*args, **kwargs)
  return v
 return decorator

def threadsafe ( function ) : lock = threading . RLock ( ) def decorator ( * args , ** kwargs ) : with lock : v = function ( * args , ** kwargs ) return v return decorator
the @threadsafe decorator ensures that no two threads execute the function simultaneously .