Categories
Blog News

Sonos Play:1 is marvelous !

Sonos Play:1 speaker
Sonos Play:1 speaker : great sound for a (relatively) small price

I just bought 2 Sonos Play:1 speakers for my living room, with the free bridge included (the offer lasts till the end of 2013, so you people who want to start with collecting Sonos speakers should hurry). Sonos speakers have been on my wish list for quite some time, ever since Vowe mentioned them on his blog, but they were always too expensive. Till now, with the Play:1 speaker coming out for a relatively cheap price of 199 euros. Still ‘ouch’ but it’s doable.

I was originally going to start out with one speaker, but due to circumstances I ended up with 2…

One speaker I originally bought from MediaMarkt.be, but at that moment they were out of stock. So I reserved one and paid an advance, hoping it would soon turn up. After 2 weeks they still didn’t have new stock, so I got a bit unhinged and in a fit I ordered from an online shop : bol.com and promptly got it delivered the next evening ! Kudos to Bol.com for their shopping experience and followup !

Then the same week I went back to MediaMarkt on other shopping needs, and guess what I found : a delivery just arrived from sonos, so I caved in and bought another one 🙂
(Two Play:1 speakers can be grouped as a stereo pair)

I had a hard time believing all the sonos hype about the setup and usage, but in my case it was all true : plug the Bridge in, plug the speakers in, install the app, follow the instructions on the app to link everything, and I was playing music in about 10 minutes (unwrapping included) !!!

A further 10 minutes I had my iTunes library linked to it (it should have been 5 minutes, but apparently you can’t have an apostrophe in your mac’s name for sonos to link up your library, and I had to look it up and rename my mac – no biggie).

You can really hear the difference to my other bog-standard Pioneer DVD 5.1 surround system when playing Jazz – when listening to Dianne Krall I could -for the first time- hear the individual sounds, even the champaign popping sound she makes !

I’m pretty happy with my Sonos setup, and I’ll guess I’ll start saving up for some other speakers in the future…

Categories
Programming

Simple Python threading.Thread example using Queue

I managed to write a really simple example of using threads in Python that I hope will give more insight on how to adapt my other programming stuff. And re-use this later on, in case I need to revisit this again it would be handy not to scour the internet again to assemble the bits and pieces of threading with Python.

The example below uses 3 threads, and processes 10 pairs of numbers (tuples) that I put in a list.

# Our list of work todo
inputlist_ori = [ (5,5),(10,4),(78,5),(87,2),(65,4),(10,10),(65,2),(88,95),(44,55),(33,3) ]

Those numbers are divided over those 3 threads by the Queue system.

The Queue system itself is limited to 5 slots, although this could easily be changed to more or less. You will notice in the console print that the message “Waiting for threads to finish.” appears after the fifth result, indicating that the queues are being used and the main program has continued on.

After putting everything in the queue system, the program waits for the threads to finish using the .join() function.

All spawned threads keep on being active, running forever, accepting jobs – that is, until the queue is empty, at which point they shut down.

I based most of my simple example on the examples in the Python threading tutorial (.pdf) work of Norman Matloff and Francis Hsu that I referenced before in a previous blog post. However, while their examples undoubtedly do more and are more extensive, they are also more complex. This example is deliberately made as simple as possible so to understand the basic principles of threading and the queue system.

Things I stumbled over:

  • Duh! You spawn the threads before you fill up the queues with stuff todo…
  • When printing out things to the console or python shell, things got jumbled because different threads took over from each other – to solve that I used the threading.Lock().acquire() and threading.Lock().release() to make sure that a thread could finish printing. Not sure if I understand completely all the possibilities this offers.
  • Still a bit stumped on getting more info, name, etc on the thread that is running at the moment – haven’t figured that out yet how to do that.

Feel free to comment and ask questions – if you can improve this program, please let me know !

# threading test
# Alex Boschmans
# www.boschmans.net
# January 2010

#
# IMPORT SECTION
#
import threading, Queue

#
# Variables setup
#
THREAD_LIMIT = 3                # This is how many threads we want
jobs = Queue.Queue(5)           # This sets up the queue object to use 5 slots
singlelock = threading.Lock()   # This is a lock so threads don't print trough each other (and other reasons)

# Our list of work todo
inputlist_ori = [ (5,5),(10,4),(78,5),(87,2),(65,4),(10,10),(65,2),(88,95),(44,55),(33,3) ]

#
# This is called from the main function
# It spawns the threads, fills up the queue with work items that the threads will use
# And then waits for the threads to finish
# This could use some more try:except code...
#
def draadje(inputlist):
    print "Inputlist received..."
    print inputlist

    # Spawn the threads
    print "Spawning the {0} threads.".format(THREAD_LIMIT)
    for x in xrange(THREAD_LIMIT):
        print "Thread {0} started.".format(x)
        # This is the thread class that we instantiate.
        workerbee().start()

    # Put stuff in queue
    print "Putting stuff in queue"
    for i in inputlist:
        # Block if queue is full, and wait 5 seconds. After 5s raise Queue Full error.
        try:
            jobs.put(i, block=True, timeout=5)
        except:
            singlelock.acquire()
            print "The queue is full !"
            singlelock.release()

    # Wait for the threads to finish
    singlelock.acquire()        # Acquire the lock so we can print
    print "Waiting for threads to finish."
    singlelock.release()        # Release the lock
    jobs.join()                 # This command waits for all threads to finish.

#
# Main thread class - based on threading.Thread
# This class is cloned/used as a thread template to spawn those threads.
# The class has a run function that gets a job out of the jobs queue
# And lets the queue object know when it has finished.
#
class workerbee(threading.Thread):
    def run(self):
        # run forever
        while 1:
            # Try and get a job out of the queue
            try:
                job = jobs.get(True,1)
                singlelock.acquire()        # Acquire the lock
                print "Multiplication of {0} with {1} gives {2}".format(job[0],job[1],(job[0]*job[1]))
                singlelock.release()        # Release the lock
                # Let the queue know the job is finished.
                jobs.task_done()
            except:
                break           # No more jobs in the queue

#
# Executes if the program is started normally, not if imported
#
if __name__ == '__main__':
    # Call the mainfunction that sets up threading.
    draadje(inputlist_ori)

Sigh. I just finished adding spaces to show where a def ends, and the damn code highlighter removed it again. Grrrrrr. If you want a copy of the code, let me know and I’ll update this post with a zipped copy of it.

Update: just discovered the “Syntaxhighlighter evolved” plugin and updated the code – indentation now works !!!