Commit 44cbc874 authored by Joel Collins's avatar Joel Collins
Browse files

Re-synchronised capture IO

parent 489b81f3
Loading
Loading
Loading
Loading
+17 −33
Original line number Diff line number Diff line
@@ -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,20 +153,13 @@ class CaptureObject(object):
    def write(self, s):
        self.stream.write(s)

    def _stream_to_file(self):
        with self.lock:
    def flush(self):
        logging.info(f"Writing to disk {self.file}")
            with FileObjectThread(open(self.file, "wb"), 'wb')  as outfile:
        with open(self.file, "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}")

    def open(self, mode):
        return open(self.file, mode)

@@ -275,8 +261,6 @@ 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)