CyberFly
CyberFly

Reputation: 867

Django, persistance without serialization

I'm working on a Django project that uses a flatbed scanner. It takes a long time to connect to the scanner. So I'm looking for a way to re-use the scanner instance.

Serialization seems to be the go to solution for this problem. Unfortunately I can not serialize or pickle the scanner instance. I keep running into errors that tell me that that serialization failed.

Is there an alternative way to re-use the same scanner instance for multiple scans? A back-end trick or maybe some front-end magic? (Note, I know nothing about front-end development.)

We can cheat a little!

The project will be running offline on a local computer, there is no internet or network connection at all. This might give options that are otherwise insecure.

Stuff I'm using for scanning

Upvotes: 0

Views: 81

Answers (1)

CyberFly
CyberFly

Reputation: 867

After looking around for a bit I found RPyC. The tutorial provides a working example, I got it running in a couple of minutes.

The service starts and connects to the scanner. Multiple clients can call the service and scan immediately.

import rpyc
import sane


class MyService(rpyc.Service):

    def __init__(self):
        self.scanner = Scanner()

    def on_connect(self, conn):
        pass

    def on_disconnect(self, conn):
        pass

    def exposed_scan_image(self):
        self.scanner.low_dpi_scan('scanner_service')


class Scanner(object):
    """
    The Scanner class is used to interact with flatbed scanners.
    """

    def __init__(self):
        sane.init()
        self.device_name = None
        self.error_message = None
        self.device = self.get_device()

    def get_device(self):
        """
        Return the first detected scanner and set the name.

        @return: sane.SaneDev
        """
        devices = sane.get_devices()
        print('Available devices:', devices)

        # Empty list means no scanner is connect or the connected scanner is
        # already being used
        if not devices:
            self.error_message = 'Scanner disconnect or already being used.'
            print(self.error_message)
            return None

        # open the first scanner you see
        try:
            device = sane.open(devices[0][0])
        except Exception as e:
            self.error_message = e
            print(e)
            return None

        brand = devices[0][1]
        model = devices[0][2]
        self.device_name = "{brand}_{model}".format(
            brand=brand,
            model=model
        )

        print("Connected to:", self.device_name)

        # set to color scanning mode, this is not always the default mode
        device.mode = 'color'

        return device

    def low_dpi_scan(self, file_name):
        """
        Scan at 300 dpi and store as jpeg.

        @param file_name: string
        """
        image = self.scan_image(300)
        image.save(file_name+'.jpeg')

    def scan_image(self, dpi):
        """
        Scan an image.

        @param dpi: integer
        @return: image file
        """
        self.device.resolution = dpi
        self.device.start()
        image = self.device.snap()

        return image


if __name__ == "__main__":
    from rpyc.utils.server import ThreadedServer
    t = ThreadedServer(MyService(), port=18861)
    t.start()


Upvotes: 1

Related Questions