kyled2012
Posts: 17
Joined: Wed Feb 06, 2013 11:43 am

Simultaneous python commands?

Wed Feb 06, 2013 11:53 am

Hello all.
I have written a python script that uses the GPIO to flash some LEDs and play a sound when a switch is opened (for a proof of concept burglar alarm).
The lights work and the sound plays, but unfortunately when the sound is played, no other commands are processed at the same time, leading to a delay until the sound is finished playing.

I would love to know, is it possible to have multiple commands executed simultaneously in python?
Or if not, is there a sound player available on the pi that can play a file without hanging terminal during playback?

Thanks in advance guys!

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Simultaneous python commands?

Wed Feb 06, 2013 1:07 pm

Hi and welcome to the forum,
please post your complete script and remember to use the

Code: Select all

 tab - then we can see how you are playing the tune.

Rgds,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Simultaneous python commands?

Wed Feb 06, 2013 1:45 pm

What you are wanting to accomplish can accomplished using Python threads -- parallel execution of tasks.

In its simplest form, you define a Python function to execute your task and use Thread to start it. Control returns back to your main application while the thread runs. You can monitor the running state of the thread using isAlive() or is_alive().

Simple example:

Code: Select all

#!/usr/bin/env python

import time
from threading import Thread

def myfunc(count):
    print "myfunc entry"
    for i in range(count):
        time.sleep(2)
        print "loop:",(i+1)

def main():
    print "main"
    t = Thread(target=myfunc, args=(10,))
    print "Starting myfunc() thread"
    t.start()
    print "main still running"
    while True:
        time.sleep(1)
        print "tick"
        time.sleep(1)
        print "tock"
        if (not t.isAlive()):
            t.join()
            print "thread stopped"
            break;

    print "Done"

if __name__=='__main__':
    main()
This just shows how threads work. Read more in the Python documentation. For your requirement, you may want to start a "playback" thread that waits on a queue to receive the next sound to play (read about interprocess communication).

Enjoy!
Bill

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Simultaneous python commands?

Wed Feb 06, 2013 2:52 pm

Threads would definitely do it, but it maybe just as simple as invoking the play command in the background (if using an external player)...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Simultaneous python commands?

Wed Feb 06, 2013 7:51 pm

Threads are pretty easy to use, mind. When I first came across it, I thought 'oh no, this'll be complicated', but I had it up and running in no time. Probably come in handy later on, too.
note: I may or may not know what I'm talking about...

kyled2012
Posts: 17
Joined: Wed Feb 06, 2013 11:43 am

Re: Simultaneous python commands?

Wed Feb 06, 2013 10:27 pm

Thanks bgreat, I will check it out when I get home tonight, seems like a perfect solution to my problem

Return to “Python”