[英]how to converting a for loop with await into asyncio.gather()
how do I write the following piece of code using asyncio.gather and map?如何使用 asyncio.gather 和 map 编写以下代码?
for i in range(len(data)):
candlestick = data[i]
candlesticks = data[0: i + 1]
await strategy.execute(candlesticks, candlestick.startTime)
You could do it like this:你可以这样做:
from asyncio import gather, create_task
tasks = []
for i in range(len(data)):
candlestick = data[i]
candlesticks = data[0: i + 1]
tasks.append(create_task(strategy.execute(candlesticks, candlestick.startTime)))
results = await gather(*tasks, return_exceptions=False)
If you want to use map()
specifically, you could do this:如果你想专门使用map()
,你可以这样做:
from asyncio import gather, create_task
await gather(
*map(
lambda i: create_task(
strategy.execute(data[0: i + 1], data[i].startTime)
),
range(len(data))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.