私は、asyncio
がリリースされる前に、ジェネレータベースのコルーチンを使用してきました。await awaitable_objectの値は何ですか?
今、私はPython 3.5で導入された新しいasync/await
の機能を学ぼうとしています。これは私のテストプログラムの一つです。
class Await3:
def __init__(self, value):
self.value = value
def __await__(self):
return iter([self.value, self.value, self.value])
async def main_coroutine():
x = await Await3('ABC')
print("x =", x)
def dummy_scheduler(cobj):
snd = None
try:
while True:
aw = cobj.send(snd)
#snd = 42
print("got:", aw)
except StopIteration:
print("stop")
dummy_scheduler(main_coroutine())
その出力は次のとおり
got: ABC
got: ABC
got: ABC
x = None
stop
x
の値がawait awaitable_object
式の結果です。この値はなぜNone
で、私はそれを私が望む値に設定するのですか?
私が見つけたのは、await couroutine()
という値は、コルーチンの戻り値によって決まることですが、それは私の場合ではありません。
コメントを外すsnd = 42
は機能しません。エラーは、手動__await__
メソッドを持つクラスを実装するつもりなら、あなたのクラスのインスタンスでawait
式の戻り値は、最後にStopIteration
例外を構築するために使用したものは何でも、引数になりますAttributeError: 'list_iterator' object has no attribute 'send'