Три способа выполнить множество задач с 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
📩#BNBUSDT 30m | Mid-Term
📉Long Entry Zone: 560.11-543.61
🎯 - Strategy Accuracy: 89%
⏳ - Signal details:
Target 1: 564.04
Target 2: 567.41
Target 3: 570.77
Target 4: 580.86
_
🧲Trend-Line: 543.61
❌Stop-Loss: 540.34
💡After reaching the first target you can put the rest of the position to breakeven
📩#BNBUSDT 30m | Mid-Term
📉Long Entry Zone: 566.42-543.30
🎯 - Strategy Accuracy: 89.78%
Last 5 signals: 100.0%
Last 10 signals: 100.0%
Last 20 signals: 100.0%
⏳ - Signal details:
Target 1: 569.14
Target 2: 571.86
Target 3: 574.58
Target 4: 582.73
_
🧲Trend-Line: 543.30
❌Stop-Loss: 540.70
💡After reaching the first target you can put the rest of the position to breakeven
After falling from the support level of $500.0, #BinanceCoin reached the support level of $400.0 and recovered and is now trading above both levels.
As you can see in the chart, a dynamic resistance still prevents #BNBUSDT growth, so in the first step, crossing and breaking this resistance can be a sign of the continuation of the upward trend in the daily time frame.
So far, BNBUSDT has hit the dynamic resistance 2 times and failed to break it, these collisions may reach up to 5 times and then the price will start to grow. The current price movement scenario is between the $400.0 support and the dynamic resistance.
📩#BNBUSDT 30m | Mid-Term
📉Short Entry Zone: 619.76-636.57
🎯 - Strategy Accuracy: 93.69%
Last 5 signals: 100.0%
Last 10 signals: 95.0%
Last 20 signals: 96.77%
⏳ - Signal details:
Target 1: 616.04
Target 2: 612.32
Target 3: 608.60
Target 4: 597.45
_____
🧲Trend-Line: 636.57
❌Stop-Loss: 640.39
💡After reaching the first target you can put the rest of the position to breakeven
#ID20000037367