Три способа выполнить множество задач с 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
💥Brand new alpha version of #Cetus is NOW LIVE on #Sui Devnet!!
🫱Access our latest application via https://app.cetus.zone
#DeFi#AMM#CLMM
https://twitter.com/CetusProtocol/status/1592871290599542785?s=20&t=THmh2WKC7kM7X0PfXpqjEQ
@everyone
Today we explore how TONCO — the first DEX with concentrated liquidity on 🤑 TON — lets you earn high yields from liquidity pools.
📹Check this video!
Dive into DeFi and start earning!
🪙
#Web3#Crypto#DeFi#DEX#LiquidityPools#AMM#TONCO
After the horse , what comes next?
yes , a Cow
New farm on #TAPSWAP OASIS pool, A single asset staking where you can stake $BIFI and earn $TAPS!
Check it out!✍️
https://dapp.tapswap.money/#/pools
#HotDeFiSummer#BSC#AMM#YieldFarming#ApeSwap
📌GPT360 is excited to announce its new media partners, who will also be integrating our protocol into their community!
📌FamSwap, A #Web3 one-stop decentralized trading platform including #AMM, #MiningPool, #LaunchPad , #Game, #NFT and #AIGC
Bitcoin Whales Buy the Dip!
Bitcoin whales are buying during the price dip. Circle CEO Jeremy Allaire advocates for U.S. regulation of stablecoins, insisting companies must register if offering dollar-backed stablecoins in the U.S. PayPal plans to integrate its stablecoin PYUSD into various products, aiming for global payments via Hyperwallet in 2025. Pump.fun is launching its own AMM and leaving Raydium.
Read more on Circle's stance here.
For PayPal's integration plans, click here.
Details about Pump.fun's move can be found here.
#Bitcoin#Stablecoin#Regulation#Circle#PayPal#PYUSD#DeFi#Crypto#AMM#Raydium#Finance#ConsumerProtection#Blockchain#Payments#MarketTrends#Investment#TechNews#VC