Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
¡New build available!
#Matrixx | #diting
Device: Xiaomi 12T Pro | Redmi K50 Ultra
Build Date:2024-03-14
Downloads:
Link |
Changelog:
ROM LOG
- Include genshin fire animation
- Fixed some jittering and lags
- improve the performance
- Sync with last source of matrix
- Bug fixes
- Now ota works
Notes
• Gapps are included
• FW included , meme cam
• Remebering, Don't report bugs without logs.
• CTS pass by default
By @TeamMEX_XDA
Donate PayPal
¡New build available!
#Matrixx | #diting
Device: Xiaomi 12T Pro | Redmi K50 Ultra
MD5SUM:a9a8f235f3a97a076ff3271f145d62c9
Build Date:2024-02-01
Downloads:
Link |
Changelog:
- Fixed eSIM issues (now eSIM should work without more problems)
- Fixed recovery issues no more fstboot when u try to reboot to recovery
- Fixed some internal crashes
Notes
• Gapps are included
• FW included , meme cam
• Remebering, Don't report bugs without logs.
• CTS pass by default
• Clean flash required
Thnks to @Mohammadnt96 for help me with some ideas and tests.
By @TeamMEX_XDA
Donate PayPal
¡New build available!
#Matrixx | #diting
Device: Xiaomi 12T Pro | Redmi K50 Ultra
MD5SUM:423c046d9878a3fc6ca7682639ee2ba1
Build Date:2024-01-19
Downloads:
Link |
Changelog:link
Notes
• Gapps are included
• GTX8_SPI for not working
• Remebering, Don't report bugs without logs.
• CTS pass by default
By @TeamMEX_XDA
Donate PayPal
¡New build available!
#Matrixx | #diting
Device: Xiaomi 12T Pro | Redmi K50 Ultra
MD5SUM:f576c0244e8cc653468173e082e9d96c
Build Date:2024-01-12
Downloads:
Link |
Changelog:
- Fixed codecs issues
- Screenrecord issue was fixed
- Netflix HDR is now working
- Fixed Netflix play issue
- revert some blobs to miui
- Introduce displayfeature color service and use MIUI color modes
- Set standard display color mode in AOD
Notes
• Gapps are included
• GTX8_SPI for not working
• Remebering, Don't report bugs without logs.
• CTS pass by default
By @TeamMEX_XDA
Donate PayPal
¡New build available!
#Matrixx | #diting
Device:Xiaomi 12T Pro | Redmi K50 Ultra
MD5SUM:35348bd7a59d0bf34e4219382862d476
Build Date:2023-12-27
Downloads:
Link |
Changelog:
- Initial public release
- Sepolicy enforcing
Notes
• Gapps are included
• FOD Not working on gtx8_spi touch screen
• Remebering, Don't report bugs without logs.
• CTS pass by default
By @TeamMEX_XDA
Donate PayPal