lock_util.py 819 B

1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. """
  3. import logging
  4. import threading
  5. class LockManager:
  6. """
  7. 全局锁管理,每个账号只能打开一个上下文相同的浏览器
  8. """
  9. def __init__(self):
  10. self.locks = {}
  11. def acquire_lock(self, key):
  12. if key not in self.locks:
  13. self.locks[key] = threading.Lock()
  14. acquire = self.locks[key].acquire(timeout=300)
  15. if acquire:
  16. logging.info(f"{key} 获取锁成功")
  17. def release_lock(self, key):
  18. if key in self.locks:
  19. self.locks[key].release()
  20. logging.info(f"{key} 释放锁成功")
  21. def is_locked(self, key):
  22. """
  23. 检查给定的键是否处于锁定状态
  24. """
  25. if key in self.locks:
  26. return self.locks[key].locked()
  27. else:
  28. return False