How far can I push ThreadingHTTPServer for long-lived streams?

November 17, 2026 4 answers
Asked by Fia

I have a standard-library HTTP server handling short JSON requests, long media transfers, and live streams. It limits concurrent connections with a semaphore, but live streams also use a watcher and several worker threads.

What should I measure or change before switching to a different server architecture?

Answers

3u~velumparcel_619

Start with measurements, not a rewrite. Track active threads, memory use, request latency, failed streams, and how often the connection cap rejects requests. Check those numbers while several devices are streaming, not just when the server is idle.

If the numbers stay comfortable and playback is reliable, the existing design may be enough for a small household. A switch becomes easier to justify when you can point to a resource limit or failure pattern that the current setup cannot handle.

Reply to this answer
_x84!brindlepiston.3

Count threads by stream as well as by connection. With one watcher and four workers per live stream, that is roughly five background threads per distinct stream, plus connection handlers. Clients watching the same stream can share those background workers, so it is not necessarily five extra threads per viewer.

Threads waiting on network I/O do not necessarily consume much CPU, but they do have memory and scheduling costs. If the count gets uncomfortable, reducing the workers per stream is a smaller experiment than moving the whole server to async I/O. Async is worth considering when lots of mostly idle connections make the thread model the actual bottleneck.

Reply to this answer
6e!canvasnotch_352

I'd look at timeouts and separate capacity limits first. A socket timeout can keep a dead or stalled client from tying up a handler indefinitely. Writing media in reasonably sized chunks can also make stalled writes easier to detect, though the timeout needs to allow for genuinely slow clients.

A separate limit for streaming routes can stop long transfers from consuming every connection slot and crowding out short JSON requests. The overall cap still matters, and whichever limit you use needs to be released on errors and disconnects too.

_j56~fablewhisk.7:

A socket timeout limits individual blocking operations, not necessarily the total duration of a stream. Long-lived playback can continue as long as reads and writes keep making progress within the timeout.

Reply to this answer
8r~plumratchet_753

One caveat beyond thread count: http.server is not recommended for production use. If this is exposed beyond a trusted home network, review that boundary carefully rather than treating the semaphore as a security feature.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions