Created
November 27, 2018 15:45
-
-
Save andreif/81223454f650a20b81601306123fc23b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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