From: Don Armstrong Date: Fri, 10 May 2024 04:19:29 +0000 (-0700) Subject: add a lock to the printer to avoid printing two things at once X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=25f8d29b65b8d6d8d6ce7da41614636b5920adfb;p=ptouch_web.git add a lock to the printer to avoid printing two things at once --- diff --git a/src/ptouch_web/api.py b/src/ptouch_web/api.py index 15aaf62..0d64c88 100644 --- a/src/ptouch_web/api.py +++ b/src/ptouch_web/api.py @@ -68,6 +68,9 @@ class QueueImage(QueueBase): self._temp_file = None +printer_lock = asyncio.Lock() + + async def print_queue_item(queue_item: QueueBase) -> QueueBase: """Print a queue item, capturing the results of the call. @@ -77,20 +80,21 @@ async def print_queue_item(queue_item: QueueBase) -> QueueBase: job printed """ for i in range(0, queue_item.copies): - stdout = io.StringIO - proc = await asyncio.create_subprocess_exec( - "ptouch-print", - *queue_item.ptouch_arguments(), - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - ) - (stdout, stderr) = await proc.communicate() - queue_item.cleanup() - queue_item.printed_at = datetime.now() - if proc.returncode != 0: - queue_item.failed = True - break - queue_item.job_result += stdout.decode() + async with printer_lock: + stdout = io.StringIO + proc = await asyncio.create_subprocess_exec( + "ptouch-print", + *queue_item.ptouch_arguments(), + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + (stdout, stderr) = await proc.communicate() + queue_item.cleanup() + queue_item.printed_at = datetime.now() + if proc.returncode != 0: + queue_item.failed = True + break + queue_item.job_result += stdout.decode() return queue_item