Три способа выполнить множество задач с asyncio
Функция для примера:
async def do_it(n):
await asyncio.sleep(random.uniform(0.5, 1))
return n
1. Последовательный вызов
async def main():
for i in range(100):
result = await do_it(i)
Такой вызов имеет смысл только тогда, когда результат одной задачи требуется для вызова следующей.
Если они независимы, то это антипаттерн, так как аналогичен простому синхронному вызову по очереди.
2. Упорядоченный результат
async def main():
tasks = [do_it(i) for i in range(100)]
results = await asyncio.gather(*tasks)
Выполняет корутины конкурентно и возвращает результат в виде списка.
Полезен когда требуется получить результаты в том же порядке в котором задачи отправлены.
3. Результат по мере готовности
tasks = [asyncio.create_task(do_it(i)) for i in range(100)]
for cor in asyncio.as_completed(tasks):
result = await cor
Так же выполняет корутины конкурентно, но не гарантирует порядок. Результат возвращается по мере готовности, каждый отдельно.
Полезен когда нужно обработать любой ответ как можно скорее.
#async
#LPT is consolidating within a falling wedge pattern and is currently trading above the support trendline.
A bounce from this level is possible, while a breakout of the pattern would confirm bullish momentum.
Conversely, a breakdown below the wedge could lead to further correction.
#LPT/USDT analysis :
#LPT is currently experiencing a bearish trend, trading below the 200 Exponential Moving Average (EMA). The price has encountered resistance at the 200 EMA and has broken below the established trendline, suggesting further downward momentum. A decline to test the swing low level is anticipated.
TF : 2h
Entry : $5.423
Target : $4.883
SL : $5.740
#LPT/USDT analysis :
#LPT has broken down and retested the previous support zone, which is now acting as resistance for the price. It is anticipated that the price will resume its bearish momentum from the current level and test lower levels. For a short entry, it is advisable to wait for a price retracement.
TF : 1H
Entry : $13.76
Target : $12.78
SL : $14.36
#LPT/USDT analysis :
#LPT is currently consolidating sideways in a correction phase above the 200 EMA. The price is likely to test the support zone and then bounce back from there to test the resistance zone.
TF : 1H
Entry : $14.60
Target : $15.30
SL : $14.25