如何从循环中已经 运行 的 'blocking function' 添加 coroutine/task 到循环

How to add a coroutine/task to the loop from a 'blocking function' already running in a loop

希望下面的代码能比问题标题更好地解释我想做的事情。

import asyncio
import time

loop = asyncio.get_event_loop()

class Foo(object):
    def __init__(self, num):
        self.num = num
    @property
    def number(self):
        # Somehow need to run get_number() in the loop.
        number = self.get_number()
        return number
    @asyncio.coroutine
    def get_number(self):
        yield from loop.run_in_executor(None, time.sleep, self.num)
        return self.num


@asyncio.coroutine
def runner(num):
    print("getting Foo({})".format(num))
    foo = Foo(num)
    yield from asyncio.sleep(1)
    print("accessing number property of Foo({})".format(num))
    number = foo.number
    print("Foo({}) number is {}".format(num, number))


tasks = [
    asyncio.async(runner(3)),
    asyncio.async(runner(1)),
    ]
go = loop.run_until_complete(asyncio.wait(tasks))

我不知道要在注释所在的 number 函数中做什么。我尝试了各种方法,但实际上我只是 "been throwing **** at the wall and hoping something sticks".

这是 follow-up 到 . I want to access the property without doing a yield from, as I need to access the property from a template (eg mako), and having yield from written everywhere is not ideal (probably not even possible considering mako is probably blocking). In a perfect world, I would have all of this running with a reify decorator

如果我想使用yield from,代码会很简单。

class Foo(object):
    def __init__(self, num):
        self.num = num
    @property
    @asyncio.coroutine
    def number(self):
        yield from loop.run_in_executor(None, time.sleep, self.num)
        return self.num


@asyncio.coroutine
def runner(num):
    print("getting Foo({})".format(num))
    foo = Foo(num)
    yield from asyncio.sleep(1)
    print("accessing number property of Foo({})".format(num))
    number = yield from foo.number
    print("Foo({}) number is {}".format(num, number))

#getting Foo(3)
#getting Foo(1)
#accessing number property of Foo(3)
#accessing number property of Foo(1)
#Foo(1) number is 1
#Foo(3) number is 3

我在该主题上找到了 ,但我看不出添加完成的回调如何适用于我的工作流程。

你要求的是不可能的,因为当你在你的主线程中(你想在没有 yield from 的情况下调用 foo.number 时,你需要显式地将控制权交还给主循环。这正是yield from 做什么。

否则,您需要 运行 在单独的线程中调用 foo.number 的函数,该线程将能够阻塞(不产生)并在不阻塞主循环的情况下等待 get_number 的结果

从你的阻塞函数而不是计算值,你应该 return asyncio.Future:

return loop.create_task(self.get_number())

当你在你的异步 runner 中得到这个时,你可以等待这样的结果:

number = await foo.number

完整测试用例:

def test_future():
    loop = asyncio.get_event_loop()

    async def target(x: int) -> int:
        loop.run_in_executor(None, time.sleep, 0.1)
        return x + 1

    def intermediate(x: int) -> asyncio.Future:
        return loop.create_task(target(x))

    async def main():
        future = intermediate(5)
        logger.debug('intermediate future = %r', future)
        value = await future
        assert value == 6

    try:
        loop.create_task(main())
        loop.call_later(0.5, loop.stop)
        loop.run_forever()
    finally:
        loop.close()