This commit is contained in:
zman 2025-04-08 14:07:29 -04:00
parent d00ceef282
commit 64209694e3

25
app.py
View File

@ -31,7 +31,6 @@ app.logger.setLevel(logging.DEBUG)
# Create a queue to hold the files for processing
file_queue = queue.Queue()
image_queue = queue.Queue()
lock = threading.Lock()
@ -118,20 +117,18 @@ def save_images(images, pdf_path):
# Worker thread that processes files from the queue
def file_worker():
while True:
# Wait until a file is available in the queue
file_path = file_queue.get()
if file_path is None: # Shutdown signal
break
# Process the file (convert and print)
app.logger.info(f"Processing file: {file_path}")
images = convert_pdf_to_images(file_path)
if images:
save_images(images, file_path)
else:
app.logger.error(f"Failed to convert PDF to images: {file_path}")
# Signal that the task is done
file_queue.task_done()
# Printer worker
def printer_worker():
while True:
image, path = image_queue.get()
@ -141,10 +138,24 @@ def printer_worker():
print_address_label(image, path)
image_queue.task_done()
# Start the worker thread
# Variable to track the printer worker thread
printer_worker_thread = None
# Ensure only one printer worker is created
def start_printer_worker():
global printer_worker_thread
if printer_worker_thread is None or not printer_worker_thread.is_alive():
app.logger.info("Starting printer worker thread.")
printer_worker_thread = threading.Thread(target=printer_worker, daemon=True)
printer_worker_thread.start()
else:
app.logger.info("Printer worker thread already running.")
# Start the file worker thread
threading.Thread(target=file_worker, daemon=True).start()
# Start the printer worker thread
threading.Thread(target=printer_worker, daemon=True).start()
# Start the printer worker only once
start_printer_worker()
@app.route('/upload', methods=['POST'])
def upload_file():