add some locking to deal with two bureaus wanting to use the small printer at the same time

workspace
Brendan Howell 3 years ago
parent 23a4765a9e
commit eb5c6c0046

@ -1,5 +1,6 @@
# bureau # bureau
import configparser import configparser
import fcntl
import functools import functools
import glob import glob
import inspect import inspect
@ -87,6 +88,8 @@ class LogPrinter(logging.Handler):
logging.Handler.__init__(self) logging.Handler.__init__(self)
def emit(self, record): def emit(self, record):
sm_print_lockfile = open("small_printer_lock", "w")
fcntl.lockf(sm_print_lockfile, fcntl.LOCK_EX)
if (self.printer["inep"] is None) and (self.printer["outep"] is None): if (self.printer["inep"] is None) and (self.printer["outep"] is None):
prn = printer.Usb(self.printer["vendorid"], self.printer["productid"]) prn = printer.Usb(self.printer["vendorid"], self.printer["productid"])
else: else:
@ -106,6 +109,8 @@ class LogPrinter(logging.Handler):
prn.text(out_text) prn.text(out_text)
prn.cut() prn.cut()
prn.close() prn.close()
fcntl.lockf(sm_print_lockfile, fcntl.LOCK_EX)
sm_print_lockfile.close()
class KeyValStore(object): class KeyValStore(object):
@ -418,6 +423,8 @@ class Bureau(object):
""" """
returns an instance of the small escpos printer returns an instance of the small escpos printer
""" """
self._sm_print_lockfile = open("small_printer_lock", "w")
fcntl.lockf(self._sm_print_lockfile, fcntl.LOCK_EX)
if (self.smprint["inep"] is None) and (self.smprint["outep"] is None): if (self.smprint["inep"] is None) and (self.smprint["outep"] is None):
prn = printer.Usb(self.smprint["vendorid"], prn = printer.Usb(self.smprint["vendorid"],
self.smprint["productid"]) self.smprint["productid"])
@ -429,6 +436,14 @@ class Bureau(object):
prn.profile = self.smprint["profile"] prn.profile = self.smprint["profile"]
return prn return prn
def _free_small_printer(self, printer):
"""
cleans up and unlocks the small printer
"""
printer.close()
fcntl.lockf(self._sm_print_lockfile, fcntl.LOCK_UN)
self._sm_print_lockfile.close()
def print_full(self, template, **kwargs): def print_full(self, template, **kwargs):
"""print a full page (A4) document """ """print a full page (A4) document """
@ -485,7 +500,7 @@ class Bureau(object):
prn.ln(2) prn.ln(2)
if cut: if cut:
prn.cut() prn.cut()
prn.close() self._free_small_printer(prn)
def print_small_image(self, img, linefeed=True): def print_small_image(self, img, linefeed=True):
""" """
@ -507,6 +522,7 @@ class Bureau(object):
prn.image(im, impl="bitImageColumn") prn.image(im, impl="bitImageColumn")
if linefeed: if linefeed:
prn.ln() prn.ln()
self._free_small_printer(prn)
@add_command("test") @add_command("test")
def test(self, data=None): def test(self, data=None):

Loading…
Cancel
Save