is prewarm function called once when starting live...
# ask-ai
f
is prewarm function called once when starting livekit worker? also can I define asyncio function within the prewarm function?
t
Thanks for reaching out, Mok. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! The
prewarm
function is called once per job process when starting a LiveKit worker. It helps to initialize or warm up processes, like preloading resources, before jobs are assigned. One important thing to keep in mind is that the
prewarm
function itself can’t be defined as an
async
function. It needs to be a synchronous callable, as outlined in the
WorkerOptions
interface. While you’re free to use asyncio functions elsewhere in your code, the
prewarm
function is designed specifically for synchronous setup tasks. If you do need to perform async initialization, a great place to handle that is in your
entrypoint
function, where async operations are fully supported. I hope this helps clarify things! Feel free to ask if you have any more questions. Sources: Worker options | LiveKit Docs | agent/main.py | examples/voice_agents/realtime_turn_detector.py
f
is ctx.proc shared across all jobs in the worker?
t
Thanks for your question! Just to clarify,
ctx.proc
isn’t shared across all jobs in a LiveKit worker. Each job runs in its own separate process, and
ctx.proc
(which is an instance of
JobProcess
) belongs specifically to that individual job process. Now, the
JobProcess
object does have a
userData
dictionary where you can store resources across job invocations within the same process. However, this data isn’t shared between different job processes. This isolation helps keep everything stable and ensures that one job’s issues won’t impact others. Sources: Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | Job lifecycle | LiveKit Docs | Worker lifecycle | LiveKit Docs
f
is there a way to initialise database connection in the process that is shared across all workers?
t
In LiveKit, database connections can't be directly shared across all workers because each job runs in its own subprocess due to the process isolation model. But don’t worry! You can still set up a database connection within a process and share it across components of that process by using the
UserData
class pattern. Here’s a friendly guide on how to manage your database connections smoothly: • Wrap your database connection inside a
UserData
class and initialize it in the
entrypoint
function. This way, the connection stays accessible throughout the life of that process. • Use a
ThreadPoolExecutor
for your database operations to keep the async event loop running smoothly—this is especially helpful if you’re working with SQLite. • If you’re working with distributed deployments, it’s a great idea to use message buses for real-time state sharing instead of relying solely on databases, since databases can sometimes become bottlenecks in these scenarios. If you’d like, I’m here to help with more detailed guidance or examples—just let me know! Sources: complex-agents/personal_shopper/README.md | How We Built a Globally Distributed WebRTC Mesh Network | How We Built a Globally Distributed WebRTC Mesh Network | examples/avatar_agents/audio_wave/dispatcher.py