Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#typescript#svelte#sveltekit#tailwindcss#tauri
Epicenter is a free, open-source set of local-first apps that let you own and control your data by storing everything—notes, transcripts, chats—in one simple folder using plain text and SQLite. You can use any AI model you want, customize tools, and access your data anywhere without relying on cloud services. Key apps include Whispering, which transcribes your speech locally, and epicenter.sh, a personal assistant that helps you search and interact with your data. This setup gives you privacy, flexibility, and full control over your information, avoiding locked, siloed apps and data traps. It’s great for anyone who values data ownership and open software.
https://github.com/epicenter-so/epicenter
#typescript#chatgpt#hacktoberfest#huggingface#llm#svelte#svelte_kit#sveltekit#tailwindcss#typescript
Chat UI is an open-source chat interface built with SvelteKit that lets you easily connect to different AI language models using any service that works with the OpenAI API format, such as Hugging Face, llama.cpp, Ollama, or OpenRouter[5]. You can quickly set it up on your computer by cloning the project, setting a few environment variables (like your API key and database connection), and running simple commands to start the app—no need to be an expert[5]. The main benefit is that you get a modern, customizable chat app that works with many AI models, making it simple to experiment, build, and share your own AI-powered chat experiences without starting from scratch.
https://github.com/huggingface/chat-ui