1
海峡を__delete__:のpythonがポイントに
はどうすればよいasync def
スペシャルPythonで__delete__
のようなクラスメソッド?
私は必要なぜ、この:
複数のプロセス間で共有素敵なキャッシュシステムを実装するためには、私は一度データベースからデータを取得し、キャッシュに格納し、キャッシュ内のデータを変更したいとデータがもう使用されない場合:データベースを更新します。私の問題は、それが非同期デフPythonのbuilint法には不可能であるが、外のコルーチンの呼び出しをスケジュールすることが可能であるasyncly最後の一つであるインスタンスを知るために、私は__delete__特別なメソッドを使用したい、
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...
複製:これは答えを参照してください。 http://stackoverflow.com/a/33134213/1943571 –
私はそこから来て、この答えはメソッド '__delete__'には適用されません。 –
リンクの中で非同期は非同期のマジックメソッド、* * __delete__を含む*。 –