sghfsg
This commit is contained in:
parent
d00ceef282
commit
64209694e3
27
app.py
27
app.py
@ -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()
|
||||
@ -106,7 +105,7 @@ def print_address_label(image, pdf_path):
|
||||
|
||||
def save_images(images, pdf_path):
|
||||
uuid = str(int(time.time()))
|
||||
for i,image in enumerate(images):
|
||||
for i, image in enumerate(images):
|
||||
if 'address' in pdf_path.lower():
|
||||
image.save(os.path.join(IMAGE_FOLDER, f"address_label_{uuid}_{i}.png"), "PNG")
|
||||
elif 'packing' in pdf_path.lower():
|
||||
@ -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():
|
||||
|
Loading…
x
Reference in New Issue
Block a user