22
33import atexit
44import threading
5+ from contextlib import contextmanager
56from dataclasses import dataclass
67from typing import List , Optional , Union
78
@@ -162,16 +163,15 @@ def get_movie(self, n_frames: int, exposure=None, binsize=None):
162163 return self ._get_media (request )
163164
164165 def _get_media (self , request : MediaRequest ) -> Union [np .ndarray , List [np .ndarray ]]:
165- self .block () # Stop the passive collection during request acquisition
166- self .grabber .request = request
167- self .grabber .acquireInitiateEvent .set ()
168- self .grabber .acquireCompleteEvent .wait ()
169- with self .grabber .lock :
170- media = self .requested_media
171- self .requested_media = None
172- self .grabber .request = None
173- self .grabber .acquireCompleteEvent .clear ()
174- self .unblock () # Resume the passive collection
166+ with self .blocked (): # Stop the passive collection during request acquisition
167+ self .grabber .request = request
168+ self .grabber .acquireInitiateEvent .set ()
169+ self .grabber .acquireCompleteEvent .wait ()
170+ with self .grabber .lock :
171+ media = self .requested_media
172+ self .requested_media = None
173+ self .grabber .request = None
174+ self .grabber .acquireCompleteEvent .clear ()
175175 return media
176176
177177 def update_frametime (self , frametime ):
@@ -187,6 +187,15 @@ def block(self):
187187 def unblock (self ):
188188 self .grabber .continuousCollectionEvent .clear ()
189189
190+ @contextmanager
191+ def blocked (self ):
192+ """Set `continuousCollectionEvent` within the statement scope only."""
193+ was_set_before = self .grabber .continuousCollectionEvent .is_set ()
194+ self .grabber .continuousCollectionEvent .set ()
195+ yield
196+ if not was_set_before :
197+ self .grabber .continuousCollectionEvent .clear ()
198+
190199 def continuous_collection (self , exposure = 0.1 , n = 100 , callback = None ):
191200 """Function to continuously collect data Blocks the videostream while
192201 collecting data, and only shows collected images.
0 commit comments