From 1857a998c969430b35384dbedf75ba95445ffcae Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Sun, 26 Nov 2023 15:37:09 +1100 Subject: [PATCH] reduce overhead of gevent.idle() by only yielding once per 1000 segments --- common/common/segments.py | 5 +++-- restreamer/restreamer/generate_hls.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/common/common/segments.py b/common/common/segments.py index e62c1d7..6969942 100644 --- a/common/common/segments.py +++ b/common/common/segments.py @@ -535,10 +535,11 @@ def fast_cut_range(segments, start, end, fixts=None): first = segments[0] if cut_start else None last = segments[-1] if cut_end else None - for segment in segments: + for i, segment in enumerate(segments): # Since long smart cuts can be CPU and disk bound for quite a while, # yield to give other things a chance to run. - gevent.idle() + if i % 1000 == 0: + gevent.idle() if segment is None: logging.debug("Skipping discontinuity while cutting") diff --git a/restreamer/restreamer/generate_hls.py b/restreamer/restreamer/generate_hls.py index c79e935..318ed54 100644 --- a/restreamer/restreamer/generate_hls.py +++ b/restreamer/restreamer/generate_hls.py @@ -69,9 +69,10 @@ def generate_media(segments, base_url): if segments and segments[0] is None: segments = segments[1:] - for segment in segments: + for i, segment in enumerate(segments): # For very large playlists, give other things a chance to run - gevent.idle() + if i % 1000 == 0: + gevent.idle() if segment is None: # Discontinuity. Adding this tag tells the client that we've missed something # and it should start decoding fresh on the next segment. This is required when