嗨,我有以下问题,我想执行getlastItemFromGivenInterval方法,让它在不等待请求响应的情况下快速执行,并为异步睡眠(60)在60秒的时间范围内再次执行整个过程。我得到的是在getLastItemFromGivenInterval()中等待请求结束。在
import aiohttp
import asyncio
loop = asyncio.get_event_loop()
task = loop.create_task(main())
loop.run_forever()
async def main():
async with aiohttp.ClientSession() as session:
while True:
await bc.getLastItemFromGivenInterval(session)
await asyncio.sleep(60)
async def getLastItemFromGivenInterval(session):
async with session.get(BinanceClient.base_endpoint + "/api/v1/time") as currentServerTime:
currentServerTime = await currentServerTime.json()
currentServerTime = currentServerTime['serverTime']
async with session.get(url) as res:
response = await res.json()
array = []
print(response)
return response
getLastItemFromGivenInterval放在单独的类中。 请告诉我如何在getLastItem…()方法中实现不等待效果。在
如果我没听错,您希望在后台启动
getLastItemFromGivenInterval
,并且每60秒执行一次,而不管完成需要多长时间。您可以将await
替换为create_task
,然后从不等待生成的任务:您可能还需要确保需要很长时间才能完成的任务或无限期挂起的任务(例如,由于网络故障)不会累积:
^{pr2}$相关问题 更多 >
编程相关推荐