Skip to content

Instantly share code, notes, and snippets.

@andreif
Created November 27, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreif/81223454f650a20b81601306123fc23b to your computer and use it in GitHub Desktop.
Save andreif/81223454f650a20b81601306123fc23b to your computer and use it in GitHub Desktop.
from datetime import datetime
from functools import wraps
from django.core.cache import cache
def locked(timeout):
def decorate(func):
@wraps(func)
def call(*args, **kwargs):
key = f"locked:{func.__name__}"
value = cache.get(key)
if value:
_log.error(
f"Call {key} {args} {kwargs} is blocked due to "
f"being locked at {value}. Timeout is {timeout}."
)
return
cache.set(key, datetime.now(), timeout)
return func(*args, **kwargs)
return call
return decorate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment