I'm currently working on a project that gets social network notification data from Facebook, Twitter and GMail. When a notification is received, a serial signal is sent to an Arduino UNO, which flashes an LED that corresponds to that social network.

For optimal speed, I have decided to use multithreading, through the threading module. What I would like to know is whether or not I need to use threading.Lock() and create a lock. Over IRC, KermM gave me some advice:

KermM wrote:
If you have data structures shared between threads, you should only access them when you hold a lock on them


The code is seperated into a series of classes. SocialLights governs the serial connection to the Arduino.


Code:

class SocialLights(object):

    def __init__(self, serial_port, baud_rate):
        # SERIAL CONNECTION STUFF HERE...

    def notify(self, icon):
        """Causes the specified icon to fade"""

        self._icon = icon

        if self._icon == "twitter":
            self._ser.write("t")

        elif self._icon == "facebook":
            self._ser.write("f")

        elif self._icon == "gmail":
            self._ser.write("g")

    def clear(self):
        """Turns all LEDs off"""

        self._ser.write("c")


Thus far, I have written two social network monitoring classes, one for GMail and the other for Twitter. They both follow this sort of format.


Code:
class GMailReader(threading.Thread):
    def __init__(self, username, password, lights):
       
        # AUTHENTICATION STUFF...
        self.__lights = lights

    def run(self):
        if notification:
            self.__lights.notify("gmail")


I have heavily condensed this code, but it looks roughly like this.

In the main body of code, each class is instantiated, and is attached to a SocialLights object which is also instantiated.


Code:
sl = SocialLights("/dev/ttyACM1", 9600)
sl.clear()

# Instantiate the social network classes and attatch them to 'sl'
tr = socialnetworks.TwitterReader(API_KEY, API_SECRET, ACCESS_KEY,
                                  ACCESS_SECRET, sl)

gr = socialnetworks.GMailReader(GMAIL_USERNAME, GMAIL_PWD, sl)

# Run the threads:
tr.start()
gr.start()


Should I be using threading.Lock() and using the acquire() and release() methods?
Easy way to ensure you won't cause tricky threading bugs: have no shared state. This is a simple case of a multi-producer single-consumer system, which is well-suited to the builtin Queue class.

Just give each 'Reader' and 'SocialLights' a handle to a Queue, then put() and get() messages as desired. The Queue will ensure you don't cause ugly errors.

Quote:
For optimal speed, I have decided to use multithreading, through the threading module.
Performance isn't a very compelling argument for multithreading here, since you'll be spending the majority of the time waiting for I/O in this application. More compelling is simplicity, since it's somewhat easier to say "Each source runs independently and pushes messages into the queue for transmission" than it is to interleave all the processing.

Quote:

Code:
class GMailReader(threading.Thread):
    def __init__(self, username, password, lights):
It doesn't matter in this case (as noted above, you're spending most of the time just waiting on I/O anyway), but if you need to allow multiple threads to actually execute at the same time you'll need to use multiprocessing due to the tyranny of the GIL.

This also sounds like a good application of an application-specific password for gmail (so you can easily revoke the application's access without mucking with changing your password), but that's just a minor comment.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement