Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Created July 21, 2022 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjberger/e6b5f243f861fff5c2e7d70ef6e45e28 to your computer and use it in GitHub Desktop.
Save matthewjberger/e6b5f243f861fff5c2e7d70ef6e45e28 to your computer and use it in GitHub Desktop.
Example of synchronization being used for escpos printing
from escpos import *
from PIL import Image
from io import BytesIO
import base64
import threading
class Printer:
_lock = threading.Lock()
def __init__(self, ip_address, port=9100) -> None:
self.printer = printer.Network(ip_address, port)
def paper_status(self):
self.printer.paper_status()
def is_online(self):
self.printer.is_online()
def print_file(self, path, thread_id):
with self._lock:
with open(path, "rb") as image_file:
base64_image = base64.b64encode(image_file.read())
image = Image.open(BytesIO(base64.b64decode(base64_image)))
print(f"[Thread {thread_id}] Printing chit...")
self.printer.image(image)
self.printer.cut()
print(f"[Thread {thread_id}] Printing succeeded.")
printer = Printer("192.168.0.172")
class print_thread(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
printer.print_file("chit.png", self.threadID)
threads = list()
for index in range(3):
thread = print_thread("Thread " + str(index), index, )
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment