FSGHDFG
This commit is contained in:
parent
a60cfae01c
commit
d06716ac92
27
app.py
27
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,6 +85,12 @@ def print_address_label(pdf_path, print_complete_event):
|
||||
cut=True
|
||||
)
|
||||
|
||||
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,
|
||||
@ -92,14 +99,13 @@ def print_address_label(pdf_path, print_complete_event):
|
||||
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():
|
||||
while True:
|
||||
@ -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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user