1+ import asyncio
12import socket
23from datetime import datetime , timedelta
34from time import sleep
1314)
1415from numpy import ndarray
1516
16- from OTVision .abstraction .observer import Subject
17+ from OTVision .abstraction .observer import AsyncSubject , Subject
1718from OTVision .application .config import (
1819 DATETIME_FORMAT ,
1920 Config ,
@@ -87,7 +88,7 @@ def fps(self) -> float:
8788
8889 def __init__ (
8990 self ,
90- subject_flush : Subject [FlushEvent ],
91+ subject_flush : AsyncSubject [FlushEvent ],
9192 subject_new_video_start : Subject [NewVideoStartEvent ],
9293 datetime_provider : DatetimeProvider ,
9394 frame_counter : Counter ,
@@ -153,10 +154,10 @@ async def produce(self) -> AsyncIterator[Frame]:
153154 occurrence = occurrence ,
154155 )
155156 if self .flush_condition_met ():
156- self ._notify_flush_observers ()
157+ await self ._notify_flush_observers ()
157158 self ._outdated = True
158159 self ._frame_counter .reset ()
159- self ._notify_flush_observers ()
160+ await self ._notify_flush_observers ()
160161 except InvalidRtspUrlError as cause :
161162 logger ().error (cause )
162163
@@ -209,7 +210,7 @@ def start(self) -> None:
209210 def flush_condition_met (self ) -> bool :
210211 return self .current_frame_number % self .flush_buffer_size == 0
211212
212- def _notify_flush_observers (self ) -> None :
213+ async def _notify_flush_observers (self ) -> None :
213214 frame_width = self ._get_width ()
214215 frame_height = self ._get_height ()
215216 frames = (
@@ -219,7 +220,7 @@ def _notify_flush_observers(self) -> None:
219220 )
220221 duration = timedelta (seconds = round (frames / self .fps ))
221222 output = self .create_output ()
222- self .subject_flush .notify (
223+ await self .subject_flush .notify (
223224 FlushEvent .create (
224225 source = self .rtsp_url ,
225226 output = output ,
@@ -256,7 +257,9 @@ def create_output(self) -> str:
256257 def notify_new_config (self , config : NewOtvisionConfigEvent ) -> None :
257258 try :
258259 logger ().debug ("New OTVision config detected. Flushing buffers..." )
259- self ._notify_flush_observers ()
260+
261+ # Create task to handle async flush notification
262+ asyncio .create_task (self ._notify_flush_observers ())
260263 except NoConfigurationFoundError :
261264 logger ().info ("No configuration found for RTSP stream. Skipping flushing." )
262265
0 commit comments