Switched metadata IO to greenlet

This commit is contained in:
Joel Collins 2020-06-15 10:01:31 +01:00
parent 9962f0b063
commit 2305a562d9

View file

@ -131,8 +131,9 @@ class CaptureObject(object):
# Stream for buffering capture data # Stream for buffering capture data
self.stream = io.BytesIO() self.stream = io.BytesIO()
# Event to notify when the stream has finished writing to disk # Event to notify when the stream has finished writing to disk
#self.file_ready = Event()
self.file_ready = gevent.event.Event() self.file_ready = gevent.event.Event()
# Access lock for the disk file
self.lock = gevent.lock.BoundedSemaphore()
# Store a nice ID # Store a nice ID
self.id = uuid.uuid4() #: str: Unique capture ID self.id = uuid.uuid4() #: str: Unique capture ID
@ -161,12 +162,13 @@ class CaptureObject(object):
self.stream.write(s) self.stream.write(s)
def _stream_to_file(self): def _stream_to_file(self):
logging.info(f"Writing to disk {self.file}") with self.lock:
with FileObjectThread(open(self.file, "wb"), 'wb') as outfile: logging.info(f"Writing to disk {self.file}")
outfile.write(self.stream.getbuffer()) with FileObjectThread(open(self.file, "wb"), 'wb') as outfile:
self.stream.close() outfile.write(self.stream.getbuffer())
self.file_ready.set() self.stream.close()
logging.info(f"Finished writing to disk {self.file}") self.file_ready.set()
logging.info(f"Finished writing to disk {self.file}")
def flush(self): def flush(self):
logging.debug(f"Flushing {self.file}") logging.debug(f"Flushing {self.file}")
@ -246,7 +248,7 @@ class CaptureObject(object):
self.save_metadata() self.save_metadata()
def save_metadata(self) -> None: def save_metadata(self) -> None:
gevent.get_hub().threadpool.spawn(self.synchronous_save_metadata) gevent.spawn(self.synchronous_save_metadata)
def synchronous_save_metadata(self) -> None: def synchronous_save_metadata(self) -> None:
""" """
@ -255,18 +257,20 @@ class CaptureObject(object):
global EXIF_FORMATS global EXIF_FORMATS
if self.format.upper() in EXIF_FORMATS and self.exists: if self.format.upper() in EXIF_FORMATS and self.exists:
logging.debug("Writing exif data to capture file") self.file_ready.wait()
# Extract current Exif data with self.lock:
exif_dict = piexif.load(self.file) logging.debug("Writing exif data to capture file")
# Serialize metadata # Extract current Exif data
metadata_string = json.dumps(self.metadata, cls=JSONEncoder) exif_dict = piexif.load(self.file)
logging.debug(f"Saving metadata string to file: {metadata_string}") # Serialize metadata
# Insert metadata into exif_dict metadata_string = json.dumps(self.metadata, cls=JSONEncoder)
exif_dict["Exif"][piexif.ExifIFD.UserComment] = metadata_string.encode() logging.debug(f"Saving metadata string to file: {metadata_string}")
# Convert new exif dict to exif bytes # Insert metadata into exif_dict
exif_bytes = piexif.dump(exif_dict) exif_dict["Exif"][piexif.ExifIFD.UserComment] = metadata_string.encode()
# Insert exif into file # Convert new exif dict to exif bytes
piexif.insert(exif_bytes, self.file) exif_bytes = piexif.dump(exif_dict)
# Insert exif into file
piexif.insert(exif_bytes, self.file)
@property @property
def metadata(self) -> dict: def metadata(self) -> dict: