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
|
# Create a queue to hold the files for processing
|
||||||
file_queue = queue.Queue()
|
file_queue = queue.Queue()
|
||||||
|
|
||||||
image_queue = queue.Queue()
|
image_queue = queue.Queue()
|
||||||
|
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
@ -106,7 +105,7 @@ def print_address_label(image, pdf_path):
|
|||||||
|
|
||||||
def save_images(images, pdf_path):
|
def save_images(images, pdf_path):
|
||||||
uuid = str(int(time.time()))
|
uuid = str(int(time.time()))
|
||||||
for i,image in enumerate(images):
|
for i, image in enumerate(images):
|
||||||
if 'address' in pdf_path.lower():
|
if 'address' in pdf_path.lower():
|
||||||
image.save(os.path.join(IMAGE_FOLDER, f"address_label_{uuid}_{i}.png"), "PNG")
|
image.save(os.path.join(IMAGE_FOLDER, f"address_label_{uuid}_{i}.png"), "PNG")
|
||||||
elif 'packing' in pdf_path.lower():
|
elif 'packing' in pdf_path.lower():
|
||||||
@ -118,20 +117,18 @@ def save_images(images, pdf_path):
|
|||||||
# Worker thread that processes files from the queue
|
# Worker thread that processes files from the queue
|
||||||
def file_worker():
|
def file_worker():
|
||||||
while True:
|
while True:
|
||||||
# Wait until a file is available in the queue
|
|
||||||
file_path = file_queue.get()
|
file_path = file_queue.get()
|
||||||
if file_path is None: # Shutdown signal
|
if file_path is None: # Shutdown signal
|
||||||
break
|
break
|
||||||
# Process the file (convert and print)
|
|
||||||
app.logger.info(f"Processing file: {file_path}")
|
app.logger.info(f"Processing file: {file_path}")
|
||||||
images = convert_pdf_to_images(file_path)
|
images = convert_pdf_to_images(file_path)
|
||||||
if images:
|
if images:
|
||||||
save_images(images, file_path)
|
save_images(images, file_path)
|
||||||
else:
|
else:
|
||||||
app.logger.error(f"Failed to convert PDF to images: {file_path}")
|
app.logger.error(f"Failed to convert PDF to images: {file_path}")
|
||||||
# Signal that the task is done
|
|
||||||
file_queue.task_done()
|
file_queue.task_done()
|
||||||
|
|
||||||
|
# Printer worker
|
||||||
def printer_worker():
|
def printer_worker():
|
||||||
while True:
|
while True:
|
||||||
image, path = image_queue.get()
|
image, path = image_queue.get()
|
||||||
@ -141,10 +138,24 @@ def printer_worker():
|
|||||||
print_address_label(image, path)
|
print_address_label(image, path)
|
||||||
image_queue.task_done()
|
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()
|
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'])
|
@app.route('/upload', methods=['POST'])
|
||||||
def upload_file():
|
def upload_file():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user