Три способа выполнить множество задач с 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
The Daily Gwei (🔗YouTube)
🔗ETH infinite bid, Ethereum's next 10 years and more - The Daily Gwei Refuel #845 - Ethereum Updates
Subscribe to the newsletter: https://thedailygwei.substack.com
Follow me on Twitter: https://twitter.com/sassal0x
Follow The Daily Gwei on Twitter: https://twitter.com/thedailygwei and https://twitter.com/DailyGwei_TV
Join the Discord channel: https://discord.gg/4pfUJsENcg
--------------------------------------------------
The Daily Gwei Refuel gives you a recap every week day on everything that happened in the Ethereum and crypto ecosystems over the previous 24 hours - hosted by Anthony Sassano.
--------------------------------------------------
Timestamps and links 👇
00:00 Introductory song
https://x.com/sassal0x/status/1955246612789465572
01:40 ETH treasury companies on fire
https://x.com/SERdotxyz/status/1955458142977810600
17:14 The ETH ETFs seeing record flows
https://x.com/sassal0x/status/1955461999862878368
23:21 The SEC's...
View original post
~
₿izFeed#⃣:#daily
OKX XP Marathon - S3
Run the long game 🏃🏻♀️🏃🏻♀️🏃🏻♀️
Prize:
250 USDT waiting for you to win!
🥇 1st : 60 USDT
🥈 2nd : 50 USDT
🥉 3rd : 40 USDT
🎁 4th : 30 USDT
🎁 5th : 20 USDT
🎁 6th–10th : 10 USDT each
How to earn points👇
1. Sign up on OKX (If you haven’t already)
2. Join & stay in OKX Global Discord (Must)
3. Chat actively in #general-chat to earn XP🏆 and use the hashtag # XPMarathonS3
4. Valid messages only (No spam, No links)
5. Send 'Win XP' to #daily-check-in to earn extra points 👉 100xp
6. Submit this Form for reward distribution
📆 Event Period: 4/2/2025, 9:30 AM - 13/2/2026, 6:00 PM (GMT+8)
👇
Join now
Our Socials:
⚜️Sign Up on OKX | Join Affiliate | TG | DC | Wallet TG | Wallet Announcement | OKX on Spotify | Twitter/X | YouTube
⭐️CN DC | CN TG
‼️Please check the authenticity of all OKX links or channels at OKX channel verification