蟒蛇异步专用类方法__delete__
问题描述:
海峡的一点是:蟒蛇异步专用类方法__delete__
我怎样才能async def
特价类的方法,如在python __delete__
?
为什么我需要这样的:
为了实现多进程之间共享一个不错的缓存系统,我想从数据库中检索一次数据,并将它们存储在缓存,缓存中的修改数据和当数据不再使用时:更新数据库。我的问题是,为了知道哪些情况下是最后一个,我想用__delete__特殊方法asyncly
def asyncinit(cls):
"""Credits: http://stackoverflow.com/a/33140788/4241798"""
__new__ = cls.__new__
async def init(obj, *arg, **kwarg):
await obj.__init__(*arg, **kwarg)
return obj
def new(cls, *arg, **kwarg):
obj = __new__(cls, *arg, **kwarg)
coro = init(obj, *arg, **kwarg)
return coro
cls.__new__ = new
return cls
@asyncinit
class AsyncUser:
async def __init__(self, id: int):
self.id = id
with await cachepool as cache:
cache.hincr(f"users:{id}", "refcnt")
async def __delete__(self):
"""Won't work"""
with await cachepool as cache:
refcnt = await cache.hincrby(f"users:{self.id}", "refcnt", -1)
if refcnt == 0:
# update database
# rest of the class...
答
这是不可能的异步高清python的builint方法,但有可能以外安排协同程序调用使用loop.create_future
或asyncio.ensure_future
的回路
class asyncdel:
def __delete__(self):
asyncio.ensure_future(free(self.__dict__.copy()))
async def free(data):
pass
Duplicate:请参阅本文以获得答案。 http://stackoverflow.com/a/33134213/1943571 –
我来自那里,这个答案不适用于'__delete__'这个方法' –
它说在那里的异步不适用于非异步魔术方法,*包括*'__delete__'。 –