【发布时间】:2020-09-25 10:43:09
【问题描述】:
对不起,如果看起来很基本,但我真的不明白为什么这会返回 None:
import asyncio
def syncFunc():
async def testAsync():
return 'hello'
asyncio.run(testAsync())
# in a different file:
x = syncFunc()
print(x)
# want to return 'hello'
如何从 asyncio.run 返回 'hello'?
这可行,但不是我想要的:
def syncFunc():
async def testAsync():
print('hello')
asyncio.run(testAsync())
syncFunc()
【问题讨论】:
-
Python 函数默认返回
None。将return添加到syncFunc即可获得结果。 -
是的(我自己的facepaml)。我认为这是异步特定问题。谢谢!
标签: python-3.x multithreading asynchronous async-await python-asyncio