From b3c4ff8f2d609cc2494f884254d678049595a331 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Thu, 1 May 2025 10:43:07 -0500 Subject: [PATCH] Finding more timers that can be converted to gevents. --- apps/hdhr/ssdp.py | 3 ++- apps/proxy/ts_proxy/stream_buffer.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/apps/hdhr/ssdp.py b/apps/hdhr/ssdp.py index 660d9c2f..d794799a 100644 --- a/apps/hdhr/ssdp.py +++ b/apps/hdhr/ssdp.py @@ -2,6 +2,7 @@ import os import socket import threading import time +import gevent # Add this import from django.conf import settings # SSDP Multicast Address and Port @@ -59,7 +60,7 @@ def ssdp_broadcaster(host_ip): sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) while True: sock.sendto(notify.encode("utf-8"), (SSDP_MULTICAST, SSDP_PORT)) - time.sleep(30) + gevent.sleep(30) # Replace time.sleep with gevent.sleep def start_ssdp(): host_ip = get_host_ip() diff --git a/apps/proxy/ts_proxy/stream_buffer.py b/apps/proxy/ts_proxy/stream_buffer.py index f0be1c52..a5169c3a 100644 --- a/apps/proxy/ts_proxy/stream_buffer.py +++ b/apps/proxy/ts_proxy/stream_buffer.py @@ -12,6 +12,7 @@ from .config_helper import ConfigHelper from .constants import TS_PACKET_SIZE from .utils import get_logger import gevent.event +import gevent # Make sure this import is at the top logger = get_logger() @@ -236,8 +237,8 @@ class StreamBuffer: timers_cancelled = 0 for timer in list(self.fill_timers): try: - if timer and timer.is_alive(): - timer.cancel() + if timer and not timer.dead: # Changed from timer.is_alive() + timer.kill() # Changed from timer.cancel() timers_cancelled += 1 except Exception as e: logger.error(f"Error canceling timer: {e}") @@ -325,8 +326,7 @@ class StreamBuffer: if self.stopping: return None - timer = threading.Timer(delay, callback, args=args, kwargs=kwargs) - timer.daemon = True - timer.start() + # Replace threading.Timer with gevent.spawn_later for better compatibility + timer = gevent.spawn_later(delay, callback, *args, **kwargs) self.fill_timers.append(timer) return timer