From c53baccb3880e0017a5d02d579d00a0ebdc70689 Mon Sep 17 00:00:00 2001 From: zman Date: Mon, 7 Apr 2025 17:22:26 -0400 Subject: [PATCH] d --- app.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 131fb50..a9c8e30 100644 --- a/app.py +++ b/app.py @@ -33,6 +33,9 @@ printer_model = "QL-1100" backend = 'pyusb' printer = 'usb://0x04f9:0x20a7' +# Create a lock to manage printer access +printer_lock = threading.Lock() + # Convert PDF to image def convert_pdf_to_image(pdf_path): """Converts a PDF to a PIL Image""" @@ -76,17 +79,22 @@ def print_address_label(pdf_path): 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("Waiting to acquire printer lock...") + with printer_lock: # Ensure only one thread sends a job at a time + app.logger.info("Sending to printer...") + try: + send( + instructions=instructions, + printer_identifier=printer, + backend_identifier=backend, + blocking=True + ) + app.logger.info("Print job sent successfully") + except Exception as e: + app.logger.error(f"Error during printing: {str(e)}") except Exception as e: - app.logger.error(f"Error during printing: {str(e)}") + app.logger.error(f"Error during label printing: {str(e)}") # Worker thread that processes files from the queue def process_queue():