python-asyncio TypeError: object dict can't be used in 'await' expression
python-asyncio TypeError: object dict can't be used in 'await' expression
我正在使用第三方模块从 API 检索数据。我只是想异步等待模块 return 偶尔需要几秒钟并冻结我的应用程序的数据。但是,当我尝试等待对该模块的调用时,我收到了 TypeError:
TypeError: object dict can't be used in 'await' expression
import thirdPartyAPIwrapper
async def getData():
retrienveData = await thirdPartyAPIWrapper.data()
return await retrieveData
def main():
loop = asncio.get_event_loop()
data = loop.run_until_complete(getData())
loop.close
return data
为什么我不能等待类型('dict')?有没有解决的办法?
如果 async/await with asyncio 不能与不 return 协程的第三方模块一起使用,那么我的其他选择是什么?
只能等待异步(用 async def
定义)函数。整个想法是这样的函数是用特殊的方式编写的,这使得 运行 (await
) 它们成为可能而不会阻塞事件循环。
如果你想从需要相当长的执行时间的普通函数(用 def
定义)中获取结果,你有以下选项:
- 将整个函数重写为异步
- 在另一个线程中调用此函数并异步等待结果
- 在另一个进程中调用此函数并异步等待结果
通常你想选择第二个选项。
下面是如何操作的示例:
import asyncio
import time
from concurrent.futures import ThreadPoolExecutor
_executor = ThreadPoolExecutor(1)
def sync_blocking():
time.sleep(2)
async def hello_world():
# run blocking function in another thread,
# and wait for it's result:
await loop.run_in_executor(_executor, sync_blocking)
loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())
loop.close()
请阅读 了解 asyncio 的工作原理。我想这会对你有很大帮助。
由于 thirdPartyAPIWrapper.data()
是一个普通的同步函数,您应该在另一个线程中调用它。
asgiref
库中有一个辅助函数。
假设我们有一个带有参数的阻塞函数:
import asyncio
import time
from asgiref.sync import sync_to_async
def blocking_function(seconds: int) -> str:
time.sleep(seconds)
return f"Finished in {seconds} seconds"
async def main():
seconds_to_sleep = 5
function_message = await sync_to_async(blocking_function)(seconds_to_sleep)
print(function_message)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
该库中还有一个 async_to_sync
辅助函数。
我正在编写测试用例,我需要模拟异步功能。所以,你可以像这样写一个简单的辅助函数。
async def resolve(val):
return val
现在你可以等待任何事情
foo = resolve(1)
await foo # No error!
你不需要异步等待哪个函数运行
async def getData():
retrienveData = thirdPartyAPIWrapper.data()
return retrieveData
我正在使用第三方模块从 API 检索数据。我只是想异步等待模块 return 偶尔需要几秒钟并冻结我的应用程序的数据。但是,当我尝试等待对该模块的调用时,我收到了 TypeError:
TypeError: object dict can't be used in 'await' expression
import thirdPartyAPIwrapper
async def getData():
retrienveData = await thirdPartyAPIWrapper.data()
return await retrieveData
def main():
loop = asncio.get_event_loop()
data = loop.run_until_complete(getData())
loop.close
return data
为什么我不能等待类型('dict')?有没有解决的办法? 如果 async/await with asyncio 不能与不 return 协程的第三方模块一起使用,那么我的其他选择是什么?
只能等待异步(用 async def
定义)函数。整个想法是这样的函数是用特殊的方式编写的,这使得 运行 (await
) 它们成为可能而不会阻塞事件循环。
如果你想从需要相当长的执行时间的普通函数(用 def
定义)中获取结果,你有以下选项:
- 将整个函数重写为异步
- 在另一个线程中调用此函数并异步等待结果
- 在另一个进程中调用此函数并异步等待结果
通常你想选择第二个选项。
下面是如何操作的示例:
import asyncio
import time
from concurrent.futures import ThreadPoolExecutor
_executor = ThreadPoolExecutor(1)
def sync_blocking():
time.sleep(2)
async def hello_world():
# run blocking function in another thread,
# and wait for it's result:
await loop.run_in_executor(_executor, sync_blocking)
loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())
loop.close()
请阅读
由于 thirdPartyAPIWrapper.data()
是一个普通的同步函数,您应该在另一个线程中调用它。
asgiref
库中有一个辅助函数。
假设我们有一个带有参数的阻塞函数:
import asyncio
import time
from asgiref.sync import sync_to_async
def blocking_function(seconds: int) -> str:
time.sleep(seconds)
return f"Finished in {seconds} seconds"
async def main():
seconds_to_sleep = 5
function_message = await sync_to_async(blocking_function)(seconds_to_sleep)
print(function_message)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
该库中还有一个 async_to_sync
辅助函数。
我正在编写测试用例,我需要模拟异步功能。所以,你可以像这样写一个简单的辅助函数。
async def resolve(val):
return val
现在你可以等待任何事情
foo = resolve(1)
await foo # No error!
你不需要异步等待哪个函数运行
async def getData():
retrienveData = thirdPartyAPIWrapper.data()
return retrieveData