Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
from fastapi.concurrency import run_in_threadpool
async def execute(data: DataRequest) -> DataResponse:
try:
result = await run_in_threadpool(sync_function, data)
return DataResponse(data=result)
except Exception as e:
return DataResponse(
error=str(e),
success=False,
)
В общем работает нормально. Для всех вызовов под капотом используется общий тредпул, всё работает предсказуемо.
Но потребовалось изменить количество запускаемых в пуле потоков (по умолчанию создается 40 воркеров).
Так как дело происходит с FastAPI, делается это через lifespan используя настройки anyio:
import anyio
@asynccontextmanager
async def lifespan(app: FastAPI):
limiter = anyio.to_thread.current_default_thread_limiter()
limiter.total_tokens = 100
yield
# если вдруг нужно вернуть обратно
limiter.total_tokens = 40
Зачем менять количество воркеров?
- уменьшить, если оперативки мало (один тред занимает ~8мб)
- увеличить чтобы выдержать нагрузку
Если есть предложения получше при тех же вводных - предлагайте😉
#async
⭐️#Nodepay Airdrop: Learn To ai And Earn #Airdrop
1. Register At NodePay
2. download the chrome extension in. pc/android (kiwi/mises browser)
3. login with your account
4. Browse Internet to earn
5.Enjoy
@AirdropGoldChannel
#Nodepay Airdrop: Learn To ai And Earn #Airdrop
1. Register
2. download the chrome extension in. pc/android (kiwi/mises browser)
3. login with your account
4. Browse Internet to earn
5.Enjoy
NodePay Airdrop Listing Date: Latest 2024 Release Details
Discover when NodePay airdrop listing date will be announced and stay updated on this promising DeFi project's token distribution and exchange availability in 2024 Contact admin if you have any questions : @Riley_Fe
#airdrops#bitcoin#blockchain#crypto#Nodepay#technology
When Will New Nodepay Listing Date Be Announced?
Discover when Nodepay listing date will be announced and learn about this innovative payment platform's journey toward public trading. Stay updated on market entrance details Contact admin if you have any questions : @Riley_Fe
#airdrops#bitcoin#blockchain#crypto#Nodepay#technology
NodePay Airdrop Date: New Token Distribution Info
Stay informed about the nodepay airdrop date and distribution schedule. Get essential details on eligibility criteria, token allocation, and how to participate in this upcoming crypto event Contact admin if you have any questions : @Riley_Fe
#airdrops#bitcoin#blockchain#crypto#Nodepay#technology
NodePay Airdrop: Claim Free Crypto Rewards Now
Join the NodePay airdrop and earn free crypto rewards by participating in simple tasks. Get your share of tokens by completing social media activities and community engagement Contact admin if you have any questions : @Riley_Fe
#airdrops#bitcoin#blockchain#crypto#Nodepay#technology
NodePay Airdrop Listing Date: New Upcoming 2024
Stay updated on the nodepay airdrop listing date and upcoming schedule for 2024. Get ready to participate in this highly anticipated token distribution event and secure your rewards Contact admin if you have any questions : @Riley_Fe
#airdrops#blockchain#crypto#defi#Nodepay#technology
AI Startups Secure Major Funding Deals
🚀
1. Inferix: Decentralized GPU network raising $2.60M led by DePIN X.
2. PoSciDonDAO: Receives undisclosed funding from BoostVC for personalized medicine.
3. Skynet: Autonomous AI agent protocol secures $1.20M in Pre-seed funding.
4. Nodepay: AI training decentralized network raises $7M, led by IDG Capital and MVP.
5. The Grid: Web3 metadata directory collects $624K in Angel funding.
For more on the funding rounds: source
#AI#Funding#Inferix#PoSciDonDAO#Skynet#Nodepay#TheGrid#DeFi#Crypto#VC
January Listing 🔥
✅#Nodepay: 6 January
✅#MomoAi: 6 January
✅#Xterio: 8 January
✅#Sidekick : 8 January
✅ $Vertus :13 January
✅#Seed : 15 January
✅#Xcoin : 20 January
✅#Tapswap : 23 January
✅#Wcoin : 25 January
✅#CommunityGaming : 28 January
Unofficial Listing
✅#PAWS
✅#UXUY
✅#Yuligo
✅#MemeWar
✅#DuckChain
✅#Celia
Stay connected