Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
arter97 Kernel | Android 14 | Nothing Phone (2)
✍️ Details:
- Version: r15
- Codename: #Pong
- Released: 15/03/24
🔽 Download: Normal & KSU Version
🆘 Support Threads: TG | XDA
🔖Tags : #Stock#Kernel#arter97#U
✨Changelogs:
- Backported lazy RCU to improve power consumption
- Other minor bug fixes and changes from Google
✍️ Note:
- This kernel is only compatible with Stock A14 NOS rom.
- Report issues (if any) in the support group with appropriate logs.
😎 Maintainer : @arter97
🔔 Updates : @NothingPhone2Updates
💬 Chat : @NothingPhone2
NothingMuchROM | Android 14 | Nothing Phone (2)
Nothing OS with some handful modifications for a better experience
✍️ Details:
- Version: r15 (NOS 2.6.0)
- Codename: #Pong
- Released: 08/07/24
🔽 Download : Here
🆘 Support group: TG | XDA
⚙️ Installation: Here
🔖Tags : #Stock#ROM#arter97#U#NothingPhone2
✨Changelogs:
- Stable release based on 2.6.0
- Custom bionic updated to use the latest AOSP compiler and fix several minor issues in the math library.
😎 Developer: @arter97
🔔 Updates: @NothingPhone2Updates
💬 Chat: @NothingPhone2
NothingMuchROM | Android 14 | Nothing Phone (2)
Nothing OS with some handful modifications for a better experience
✍️ Details:
- Version: r14 (NOS 2.5.6)
- Codename: #Pong
- Released: 02/07/24
🔽 Download : Here
🆘 Support group: TG | XDA
⚙️ Installation: Here
🔖Tags : #Stock#ROM#arter97#U#NothingPhone2
✨Changelogs:
- Stable release based on NOS 2.5.6
- Dirac is back, thanks to the fix from gotenksIN
✍️ Note:
- If you don't want dirac, stick to the r13 build based on NOS 2.5.6
😎 Developer: @arter97
🔔 Updates: @NothingPhone2Updates
💬 Chat: @NothingPhone2
NothingMuchROM | Android 16 | Nothing Phone (2)
✍️ Details:
- Version: r40
- Codename: #Pong
- Released: 10/03/26
🔽 Download : Here
🆘 Support group: Here
⚙️ Installation: Here
🌳 Source: Here
🔖Tags : #Stock#ROM#arter97#V#NothingPhone2
✨ Changelogs
- Update base to 4.0 (Pong_B4.0-260226-0955)
🗒 Note:
- Back up your data before proceeding.
- Make absolutely sure that you're flashing the respective firmware alongside the installation. Failure to do so will result in an untested scenario.
- Recovery flashable firmwares can be found here.
- It is recommended to use arter97's latest kernel build with this ROM.
👤 Developer: @arter97
🔔 Updates: @NothingPhone2Updates
💬 Chat: @NothingPhone2
NothingMuchROM | Android 15 | Nothing Phone (2)
✍️ Details:
- Version: r39
- Codename: #Pong
- Released: 20/09/25
🔽 Download : Here
🆘 Support group: Here
⚙️ Installation: Here
🔖Tags : #Stock#ROM#arter97#V#NothingPhone2
✨ Changelogs
- Update base to Pong-V3.2-250917-1451
🗒 Note:
- Back up your data before proceeding.
- Make absolutely sure that you're flashing the respective firmware alongside the installation. Failure to do so will result in an untested scenario.
- Recovery flashable firmwares can be found here.
- It is recommended to use arter97's latest kernel build with this ROM.
👤 Developer: @arter97
🔔 Updates: @NothingPhone2Updates
💬 Chat: @NothingPhone2
NothingMuchROM | Android 15 | Nothing Phone (2)
✍️ Details:
- Version: r37
- Codename: #Pong
- Released:15/07/25
🔽 Download : Here
🆘 Support group: Here
⚙️ Installation: Here
✨ Changelogs: Here
🔖Tags : #Stock#ROM#arter97#V#NothingPhone2
🗒 Note:
- Back up your data before proceeding.
- Make absolutely sure that you're flashing the respective firmware alongside the installation. Failure to do so will result in an untested scenario.
- Recovery flashable firmwares can be found here.
- Stable release based on 3.2 (Pong_V3.2-250708-2227_3.2)
- It is recommended to use arter97's latest kernel with this ROM.
👤 Developer: @arter97
🔔 Updates: @NothingPhone2Updates
💬 Chat: @NothingPhone2
NothingMuchROM | Android 15 | Nothing Phone (2)
✍️ Details:
- Version: r36
- Codename: #Pong
- Released: 23/05/25
🔽 Download : Here
🆘 Support group: Here
⚙️ Installation: Here
✨ Changelogs: Here
🔖Tags : #Stock#ROM#arter97#V#NothingPhone2
🗒 Note:
- Back up your data before proceeding.
- Make absolutely sure that you're flashing the respective firmware alongside the installation. Failure to do so will result in an untested scenario.
- Recovery flahsable firmwares can be found here.
- Flashing super is enough for users coming from r35.
- Stable release based on NOS 3.0, Build: Pong-V3.0-250506-1805.
- It is recommended to use arter97's latest kernel with this ROM.
👤 Developer: @arter97
🔔 Updates: @NothingPhone2Updates
💬 Chat: @NothingPhone2