1. 2. How to prevent LiveKit from spawning multip...
# ask-ai
s
1. 2. How to prevent LiveKit from spawning multiple worker processes (the forkserver processes we see in htop) 3. What parameters control the number of Python processes created 4. How to ensure only 1 Python process runs even during high load or multiple sessions
t
Thanks for reaching out, MBMBMB. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’d love to help you prevent LiveKit from spawning multiple worker processes. You can do this by configuring the
multiprocessing_context
parameter in the
WorkerOptions
class to use the "spawn" context instead of the default "forkserver" on Linux. This simple change helps avoid creating multiple forkserver processes:
Copy code
from livekit.agents import WorkerOptions

opts = WorkerOptions(
    entrypoint_fnc=your_entrypoint_function,
    multiprocessing_context="spawn"  # Prevents forkserver usage
)
To make sure only one Python process runs even when there’s high load or multiple sessions, here are a couple of helpful tips: • Set
num_idle_processes = 1
to limit the number of pre-warmed processes. • Use a custom
load_fnc
that returns
1.0
whenever any job is active, so the worker is marked as unavailable for new jobs. Here’s an example configuration to guide you:
Copy code
async def entrypoint(ctx: JobContext):
    await ctx.connect()
    await ctx.shutdown()

def custom_load(worker):
    return 1.0 if len(worker.active_jobs) > 0 else 0.0

worker = Worker(
    opts=WorkerOptions(
        entrypoint_fnc=entrypoint,
        load_fnc=custom_load,
        load_threshold=0.7,
        num_idle_processes=1,
    )
)
This setup helps ensure that only one process runs at a time. If you have any questions or need a hand with anything else, please feel free to ask—I’m here to help! Sources: tests/test_ipc.py | Deploying to custom environments | LiveKit Docs | Docs::Agents | Worker options | LiveKit Docs | Inside a session | LiveKit Docs
s
how to truly limit the worker processes to just 1. The issue is that despite setting: • num_idle_processes=0 • load_threshold=float('inf') • Custom load_fnc that always returns 1.0 • Various environment variables We're still seeing 4-5 python main.py processes at idle. Ask them specifically: 1. How to ensure only ONE worker process is created, not multiple 2. Why setting num_idle_processes=0 still results in multiple processes 3. If there's a way to completely disable multiprocessing and run everything in a single process 4. What environment variables or configuration options truly control the number of worker processes The multiprocessing.spawn context is working (no more forkserver), but we still have too many processes at idle.
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!