Skip to content

Thread safe #9

Open
Open
@kdschlosser

Description

I am wanting to make some changes to this library that will make it thread safe. While I know I am able to do this for my own use I wanted to know if I made a PR for the changes would there be an interest in adding them to this library?

This would be my game plan.
I am going to create an instance singleton meta class to handle the creation of a Blink1 instance. This will make it so that only a single instance for a blink1 device can exist at any given point. This will allow us to handle multiple threads accessing the device at the same time by using a thread lock in each instance.

Right now If I used this code there would be a problem.

import threading

from blink1.blink1 import Blink1

b1 = Blink1(serial_number='1234567890')
b2 = Blink1(serial_number='1234567890')

def _do():
    b1.fade_to_rgb_uncorrected(5000, 75, 50, 36)

threading.Thread(target=_do).start()

b2.fade_to_rgb_uncorrected(3000, 68, 32, 158)

the way the current library is coded an exception would occur if a Blink1 instance gets created more then one time for a device with the same serial number. what should happen is if an instance for a device already exists then that instance should be returned. designing it this way has a series of benefits. A user would not have to keep reference to an instance at all. successive calls to Blink1.__init__ would return an instance of Blink1 if it already exists. we can also get creative with the Blink1 class by coding it so that it is a device and also an enumeration of all devices at the same time.

I will hammer together something and show you the code to see if it is something you like.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions