import logging from typing import Any import cachetools import time MAX_SIZE = 10000…
Jump to turn (14)
- 1import logging from typing import Any import cachetools import time MAXSIZE = 10
- 2上述的 with如果发现被锁了,会等待吗
- 3那我可以创建多个锁吗 self.lock1 = threading.Lock self.lock2 = threading.Lock lf.lock3 = th
- 4import cachetools import time MAXSIZE = 10000 30分钟 TTL = 30 60 class AttributeCa
- 5那如何手动删除一条数据吗
- 6报错 : with self.md5keylock: E AttributeError: enter
- 7python如何启动两个线程调用 somemethod 方法
- 8假如 somemethod 有参数,如何传递参数呢
- 9def testmulthreaddeletecachekey(self): # 测试线程安全 key1 = "md51" key2 = "md52" thre
- 10python中的异步方法只能被异步方法调用吗
- 11一个同步方法,调用一个异步方法,是不是也会被阻塞线程
- 12如果要实现异步不阻塞,是上线游的所有方法都需要 异步吗
- 13异步方法,如何等待5秒
- 14class CacheType(Enum): ATTRIBUTE = "attribute" FACTOR = "factor" COMPARE = "comp
- User
import logging from typing import Any
import cachetools import time MAX_SIZE = 10000
30分钟
TTL = 30 * 60
class AttributeCacheService: def init(self): # 缓存对比数据 self.ttl_cache_compare: cachetools.TTLCache = cachetools.TTLCache(maxsize=MAX_SIZE, ttl=TTL)
codedef get_cache_compare_data(self, key: str) -> Any: get = self.ttl_cache_compare.get(key, None) return get def cache_compare_data(self, key: str, value: Any): self.ttl_cache_compare[key] = valuecache_a = AttributeCacheService()
以上是一个单例模式,我已经确保 AttributeCacheService()只会创建一次,并重复值 cache_a
但是我有多个进程调用cache_a的get_cache_compare_data和cache_compare_data来操作self.ttl_cache_compare
以上可能存在线程安全问题,我想确保一次性只能有一个线程能调用get_cache_compare_data和cache_compare_data,需要等待其他线程调用完,当前线程才能调用
如何通过锁的方法来解决我的问题
- Assistant