抱歉,如果看起来很基本,但我真的不明白为什么这返回 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()
最佳答案
why this returns None:
因为在您的代码中 syncFunc
函数没有 return
语句。
这是稍微不同的版本,可以满足您的需求:
def syncFunc():
async def testAsync():
return 'hello'
return asyncio.run(testAsync()) # return result of asyncio.run from syncFunc
# in a different file:
x = syncFunc()
print(x)
关于python - 同步函数内的 asyncio.run 返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62228663/