Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safe #9

Open
kdschlosser opened this issue Aug 29, 2020 · 1 comment
Open

Thread safe #9

kdschlosser opened this issue Aug 29, 2020 · 1 comment

Comments

@kdschlosser
Copy link
Contributor

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.

@todbot
Copy link
Owner

todbot commented Aug 29, 2020

This sounds like a good idea. I've used similar singleton concepts in other blink(1) libraries. I admit to not being very Python-literate so if you can provide guidance here, I would much appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants