From 44cbc874165fd585f5f0374c9a5a2afc0ad10a24 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Fri, 3 Jul 2020 15:05:53 +0100 Subject: [PATCH] Re-synchronised capture IO --- openflexure_microscope/captures/capture.py | 50 ++++++++-------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/openflexure_microscope/captures/capture.py b/openflexure_microscope/captures/capture.py index 57db09d5..00777442 100644 --- a/openflexure_microscope/captures/capture.py +++ b/openflexure_microscope/captures/capture.py @@ -10,9 +10,6 @@ from PIL import Image import dateutil.parser import atexit -from gevent.fileobject import FileObjectThread -import gevent - from collections import OrderedDict from openflexure_microscope.camera import piexif @@ -129,10 +126,6 @@ class CaptureObject(object): """Create a new StreamObject, to manage capture data.""" # Stream for buffering capture data self.stream = io.BytesIO() - # Event to notify when the stream has finished writing to disk - self.file_ready = gevent.event.Event() - # Access lock for the disk file - self.lock = gevent.lock.BoundedSemaphore() # Store a nice ID self.id = uuid.uuid4() #: str: Unique capture ID @@ -160,19 +153,12 @@ class CaptureObject(object): def write(self, s): self.stream.write(s) - def _stream_to_file(self): - with self.lock: - logging.info(f"Writing to disk {self.file}") - with FileObjectThread(open(self.file, "wb"), 'wb') as outfile: - outfile.write(self.stream.getbuffer()) - self.stream.close() - self.file_ready.set() - logging.info(f"Finished writing to disk {self.file}") - def flush(self): - logging.debug(f"Flushing {self.file}") - gevent.spawn(self._stream_to_file) - logging.debug(f"Returning flushing {self.file}") + logging.info(f"Writing to disk {self.file}") + with open(self.file, "wb") as outfile: + outfile.write(self.stream.getbuffer()) + self.stream.close() + logging.info(f"Finished writing to disk {self.file}") def open(self, mode): return open(self.file, mode) @@ -275,20 +261,18 @@ class CaptureObject(object): global EXIF_FORMATS if self.format.upper() in EXIF_FORMATS and self.exists: - self.file_ready.wait() - with self.lock: - logging.debug("Writing exif data to capture file") - # Extract current Exif data - exif_dict = piexif.load(self.file) - # Serialize metadata - metadata_string = json.dumps(self.metadata, cls=JSONEncoder) - logging.debug(f"Saving metadata string to file: {metadata_string}") - # Insert metadata into exif_dict - exif_dict["Exif"][piexif.ExifIFD.UserComment] = metadata_string.encode() - # Convert new exif dict to exif bytes - exif_bytes = piexif.dump(exif_dict) - # Insert exif into file - piexif.insert(exif_bytes, self.file) + logging.debug("Writing exif data to capture file") + # Extract current Exif data + exif_dict = piexif.load(self.file) + # Serialize metadata + metadata_string = json.dumps(self.metadata, cls=JSONEncoder) + logging.debug(f"Saving metadata string to file: {metadata_string}") + # Insert metadata into exif_dict + exif_dict["Exif"][piexif.ExifIFD.UserComment] = metadata_string.encode() + # Convert new exif dict to exif bytes + exif_bytes = piexif.dump(exif_dict) + # Insert exif into file + piexif.insert(exif_bytes, self.file) logging.info(f"Finished saving metadata to {self.file}") @property