将 @属性 装饰器与 @asyncio.coroutine 一起使用而不可能产生 yield from 吗?
Using @property decorator with @asyncio.coroutine without doing yield from possible?
我想要一个 class 如下
Foo(object):
@property
@asyncio.coroutine
def bar(self):
# This will need to run some blocking code via loop.run_in_executor()
return 'bar'
然后我想访问属性而无需 yield from
# In a loop...
foo = Foo()
foo.bar #This will return a generator object, but I want it to return 'bar'.
yield from foo.bar #This will return 'bar', but I don't want to do the yield from.
这样的事情可能吗?
运行协程生成器的方法是使用来自另一个协程的yield from
(Python 3.5 中的await
)。 yield from
(await
) 允许一个协程驱动另一个协程,这通常意味着您拥有最终由事件循环驱动的链接协程链。
另一种选择是使用像 asyncio.async
(Python 3.5 中的ensure_future()
)这样的类似任务的包装器来驱动协程。
如果没有上述任何一项,根据您的观察,它只是一个 inert 生成器对象(或协程,在 Python 3.5 中)。
我想要一个 class 如下
Foo(object):
@property
@asyncio.coroutine
def bar(self):
# This will need to run some blocking code via loop.run_in_executor()
return 'bar'
然后我想访问属性而无需 yield from
# In a loop...
foo = Foo()
foo.bar #This will return a generator object, but I want it to return 'bar'.
yield from foo.bar #This will return 'bar', but I don't want to do the yield from.
这样的事情可能吗?
运行协程生成器的方法是使用来自另一个协程的yield from
(Python 3.5 中的await
)。 yield from
(await
) 允许一个协程驱动另一个协程,这通常意味着您拥有最终由事件循环驱动的链接协程链。
另一种选择是使用像 asyncio.async
(Python 3.5 中的ensure_future()
)这样的类似任务的包装器来驱动协程。
如果没有上述任何一项,根据您的观察,它只是一个 inert 生成器对象(或协程,在 Python 3.5 中)。