Skip to content

Instantly share code, notes, and snippets.

@GGontijo
Last active October 31, 2024 13:35
Show Gist options
  • Select an option

  • Save GGontijo/d3b3bf5795b1f1f69a5c538dea328ce4 to your computer and use it in GitHub Desktop.

Select an option

Save GGontijo/d3b3bf5795b1f1f69a5c538dea328ce4 to your computer and use it in GitHub Desktop.
Simple Threadlock
import logging
from time import sleep
class Threadlock():
'''Singleton'''
_instance = None
_locked = False
def is_locked(self):
return self._locked
def wait_unlock(self, check_interval=5, silent=False):
logging.info(f'Aguardando liberação do lock, verificando a cada {check_interval} segundos...')
while self.is_locked():
sleep(check_interval)
if not silent:
logging.info(f'Lock ainda está ativo, aguardando {check_interval} segundos...')
def lock(self):
self._locked = True
return self._locked
def unlock(self):
self._locked = False
return self._locked
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __enter__(self):
if self.is_locked():
self.wait_unlock(check_interval=5, silent=True)
self.lock()
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self.unlock()
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment