Три способа выполнить множество задач с 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
🔥 Pikaio Airdrop
Pikaio aims to be the first launchpad on the #Bitlayer network.
👉https://pika.io/points/fS7uzk
(If there are items)
1️⃣Wallet example (Metamask,OKXv.b)
2️⃣Connect your Twitter account and do the Rt, Like task.
3️⃣Connect Telegram and join the telegram channel.
That's it, the current number of tasks.
📌 There will be special rewards for early events
1.Early Token Airdrops: Catch the moment to receive airdrops of upcoming ecosystem tokens!
🆕 BitLayer is now live on TokenPocket.
🟧BitLayer is the first Bitcoin security-equivalent Layer 2 based on the BitVM paradigm.
🚀How to create/import a #BitLayer wallet on #TokenPocket Mobile Wallet and Chrome Extension Wallet?
Guide👉https://help.tokenpocket.pro/en/wallet-operation/how-to-create-a-wallet/bitlayer
💙Download #TokenPocket, create your BitLayer Wallet, and stay tuned to the upcoming campaign! 🔥
👉https://tokenpocket.pro
👉https://extension.tokenpocket.pro
🎇Happy #TP6Anni!!
💙Join and win 50 BitlayerLabs Lucky Helmet NFT Whitelists Giveaway on Galxe!
👉https://app.galxe.com/quest/TokenPocket/GCddyth8TC
🟧Bitlayer Lucky Helmet is the official NFT launched by BitlayerLabs, a symbol of identity representing the contributions of builders in the Bitlayer community.
#TP6Anni#TokenPocket#Bitlayer
🏅 Predict & Earn 50K Points Daily! 💰
🏀 Premier League Week 1 Prediction: Who will win—Man United vs. Fulham, Arsenal vs. Wolves?
👉 How to Enter:
- RT & comment with a screenshot of your prediction
- Drop your Bitlayer address
🎯 5 winners will be randomly chosen to win 10K points each tomorrow!
Join Now: http://bitlayer.org/guess-to-earn
#Preditctowin#Bitlayer#Giveaway
Retweet:
https://x.com/bitlayerlabs/status/1824353973127745582?s=46
🟧 Enjoy the complete BitlayerLabs Ecosystem on your TokenPocket Wallet!
👉https://tokenpocket.pro
You can easily
✅Bridge #Bitcoin to Bitlayer
✅Find the trending DApps on Bitlayer
✅Track the real-time market #NFA
✅Manage your NFTs #LuckyHelmet
🔸and more in the near future!
Ready your #Bitlayer Wallet on TokenPocket and stay tuned for more!
👉https://help.tokenpocket.pro/en/wallet-operation/how-to-create-a-wallet/bitlayer