1
非同期にしか設定できないプロパティを持つクラスがあるとします。明示的にセッターを呼び出さずにこの作業を行う方法はありますか?非同期プロパティセッター
MNWE:
import asyncio
loop = asyncio.get_event_loop()
class AsyncTest:
def __init__(self, attrib):
self._attrib = attrib
@property
def attrib(self):
return self._attrib
@attrib.setter
async def set_attrib(self, attrib):
await asyncio.sleep(1.0)
self._attrib = attrib
async def main():
t = AsyncTest(1)
print(t.attrib)
await t.attrib = 3
print(t.attrib)
asyncio.ensure_future(main())
loop.run_forever()
これはawait
ための文法は
await ::= ["await"] primary
あるので、私たちが忘れてバインドされていると思われることを考えると、驚くべきことではありません
File "asyncprop.py", line 22
await t.attrib = 3
^
SyntaxError: can't assign to await expression
で失敗しますこの良い@property
とゲッターとセッターを使用することを辞任非同期操作の場合私は何か見落としてますか?
@Evpok:おそらくあなた自身の 'async_property'記述子を作成する方法があります。 –
ええ、それでも 'setattr'だけで動作しますが、これはプロパティを持つ目的を破るものです。 – Evpok
@Evpok:はい、あなたは 'setattr()'を使って制限されている言語に非同期の代入が追加されるまでです。私はそのような機能が存在するのではないかと疑う。 –