]> git.donarmstrong.com Git - ptouch_web.git/commitdiff
add a lock to the printer to avoid printing two things at once
authorDon Armstrong <don@donarmstrong.com>
Fri, 10 May 2024 04:19:29 +0000 (21:19 -0700)
committerDon Armstrong <don@donarmstrong.com>
Fri, 10 May 2024 04:19:29 +0000 (21:19 -0700)
src/ptouch_web/api.py

index 15aaf623b193a59d70917328ec64c833e6c1e088..0d64c889ea677874a4c0631c4cf5304e507b14b3 100644 (file)
@@ -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