简体   繁体   English

在python asyncio中是否有理由`return await ...`?

[英]Is there ever a reason to `return await ...` in python asyncio?

I know in js it doesn't add anything to do an await before a return statement (ie return await ... ), but is it the case in python too, or this somehow makes the materialization more probable or different?我知道在 js 中它不会在return语句之前添加任何内容来执行await (即return await ... ),但是在 python 中也是如此,或者这以某种方式使物化更有可能或不同?

If the two are not equivalent, what is the best practice?如果两者不等价,最佳实践是什么?

Given:鉴于:

async def foo() -> str:
    return 'bar'

What you get when calling foo is an Awaitable , which obviously you'd want to await .调用foo时你得到的是一个Awaitable ,显然你想要await What you need to think about is the return value of your function.您需要考虑的是函数的返回值。 You can for example do this:例如,您可以这样做:

def bar() -> Awaitable[str]:
    return foo()  # foo as defined above

There, bar is a synchronous function but returns an Awaitable which results in a str .在那里, bar是一个同步函数,但返回一个Awaitable ,结果是一个str

async def bar() -> str:
    return await foo()

Above, bar itself is async and results in an Awaitable when called which results in a str , same as above.上面, bar本身是asyncAwaitable时会导致Awaitable ,这会导致str ,与上面相同。 There's no real difference between these two usages.这两种用法之间没有真正的区别。 Differences appear here:差异出现在这里:

async def bar() -> Awaitable[str]:
    return foo()

In that example, calling bar results in an Awaitable which results in an Awaitable which results in a str ;在那个例子中,调用bar导致一个Awaitable导致一个Awaitable导致一个str quite different.很不一样。 If you naïvely use the above, you'll get this kind of result:如果你天真地使用上面的,你会得到这样的结果:

>>> asyncio.run(bar())
<coroutine object foo at 0x108706290>
RuntimeWarning: coroutine 'foo' was never awaited

As a rule of thumb, every call to an async must be await ed somewhere once.根据经验,每次调用async必须在某处await一次。 If you have two async ( async def foo and async def bar ) but no await in bar , then the caller of bar must await twice, which would be odd.如果你有两个asyncasync def fooasync def bar ),但没有awaitbar ,然后调用者bar必须await两次,这将是奇数。

TL)DR of @deceze answer. TL)@deceze 的 DR 答案。

Yes, there is a reason.是的,这是有原因的。 Always return await from a coroutine when calling another coroutine.调用另一个协程时,始终从协程return await

Async functions always return an Awaitable, even with a plain return . Async函数总是返回一个 Awaitable,即使是简单的return You only get the actual result by calling await .您只能通过调用await获得实际结果。 Without return await the result is an extra wrapped Awaitable and must be awaited twice.没有return await结果是一个额外包装的 Awaitable 并且必须等待两次。 See doc.见文档。

import asyncio

async def nested():
    return 42

async def main():
    # Nothing happens if we just call "nested()".
    # A coroutine object is created but not awaited,
    # so it *won't run at all*.
    nested()

    # Let's do it differently now and await it:
    print(await nested())  # will print "42".

asyncio.run(main())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM