From d06716ac926497874d6fb6846217c033b7440f04 Mon Sep 17 00:00:00 2001 From: zman Date: Mon, 7 Apr 2025 17:53:07 -0400 Subject: [PATCH] FSGHDFG --- app.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/app.py b/app.py index 07ccaa3..6f342ba 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,7 @@ import mimetypes import logging from logging.handlers import RotatingFileHandler import time +import fcntl # For file locking app = Flask(__name__) @@ -48,7 +49,7 @@ def convert_pdf_to_image(pdf_path): return None # Function to print address labels -def print_address_label(pdf_path, print_complete_event): +def print_address_label(pdf_path): try: # Convert PDF to image image = convert_pdf_to_image(pdf_path) @@ -84,21 +85,26 @@ def print_address_label(pdf_path, print_complete_event): cut=True ) - app.logger.info("Sending to printer...") - send( - instructions=instructions, - printer_identifier=printer, - backend_identifier=backend, - blocking=True - ) - app.logger.info("Print job sent successfully") - + app.logger.info("Attempting to acquire printer lock...") + + # Open the printer device and lock it to prevent concurrent access + with open(printer, 'wb') as printer_device: + fcntl.flock(printer_device, fcntl.LOCK_EX) # Exclusive lock on printer device + try: + app.logger.info("Sending to printer...") + send( + instructions=instructions, + printer_identifier=printer, + backend_identifier=backend, + blocking=True + ) + app.logger.info("Print job sent successfully") + finally: + # Ensure the lock is released after the print job is finished + fcntl.flock(printer_device, fcntl.LOCK_UN) + except Exception as e: app.logger.error(f"Error during printing: {str(e)}") - - finally: - # Signal that the current print job is done - print_complete_event.set() # Worker thread that processes files from the queue def process_queue(): @@ -107,17 +113,10 @@ def process_queue(): file_path = file_queue.get() if file_path is None: # Shutdown signal break - - # Create an event to signal print job completion - print_complete_event = threading.Event() - # Process the file (convert and print) app.logger.info(f"Processing file: {file_path}") - print_address_label(file_path, print_complete_event) - - # Wait until the current print job is completed before processing the next one - print_complete_event.wait() - + print_address_label(file_path) + time.sleep(10) # Signal that the task is done file_queue.task_done()