3

I want to receive serial data and depending on the data want to make announcement. my monitor function will continuously monitor serial data. But I am facing a problem that when i am announcing something then after completion of announcement serial data is monitored and the process going slow. I want to monitor serial data continuously and want to made the announcement parallel. Is threading is the best option?? how to handle it?

def announce(data):
   subprocess.call('espeak',data)

while 1:

    receiveddata= xbee.readline()  
    if receiveddata=='a1':
        announce("i am ok in room1")
    if receiveddata=='b2':
        announce("Urgently attend room 1")
3
  • Use the threading package, one thread to monitor the serial data, one thread to fork the subprocess. Use Queue to communicate between the two threads will be nice enough. Commented Nov 12, 2015 at 8:19
  • Sir I am new in threading if you kindly provide me an example it will be very helpful for me. Commented Nov 12, 2015 at 8:21
  • @KenCheung I think your suggestion is FAR better than the answer provided below by Torxed: just create the announcing thread ONCE and let the main thread pass the received data to the announcing thread via a queue. Why don't you write it as a full answer? Commented Nov 12, 2015 at 8:40

1 Answer 1

1
from threading import Thread

def announce(data):
    subprocess.call('espeak',data)

class worker(Thread):
    def __init__(self, data):
        Thread.__init__(self)
        self.data = data

    def run(self):
        if receiveddata=='a1':
            announce("i am ok in room1")
        if receiveddata=='b2':
            announce("Urgently attend room 1")
        # at the end of run() the process will die.

while 1:
    receiveddata = xbee.readline()
    thread_handle = worker(receiveddata)
    thread_handle.start() # <- This starts the thread but keeps on going

Here's a skeleton framework that you can use in order to achieve paralell processing in Python. It's not complete nor is it perfect, but it will give you a start and it will solve your initial problem.

There's tons of "best practices" for threading and stuff, i'll leave google to explain and find those because there will always be a better solution than what i can produce in one short answer here.

Good to know:

I honored the fact that you were new to threading in python.

But as discussed in the comments below, this will be a resource demanding solution if you have a a lot of data on the serial port (which will create thread -> do work -> die).

There are more effective threading solutions (such as keeping a thread alive throughout the program and calling a function in the class that announces instead). But this is the bare minimum that you'll need in order to get started.

Once you're comfortable with threads

I'll leave these links here for you to experiment and evolve your threading knowledge from this very basic example above:

Working with queues

Sign up to request clarification or add additional context in comments.

6 Comments

please follow PEP-8 guidelines for naming conventions.
Where is receiveddata defined/assigned?
@Pynchia data = xbee.readline(), i simply renamed the recieveddata to data and passed that to the class (that will become a thread).
@Pynchia I'm sorry but i consider single word classes ugly when they begin with a capital letter. It's a matter of taste and i respect that the majority of people think this is awesome.. But it's like religion, don't force it down my throat if I don't like it, it hinders my productivity trying to focus on things that are not important to me. It's selfish I know, but if i'm to be able to do my every day work, help people here and be a tutor I will do what I feel comfortable with. I do however normally teach people to follow the PEP guidelines, I just don't apply them to quick answers.
See my comment to the question above. Personally I think there is a more elegant solution than creating a thread upon every reception, which is overkill. This is a classic producer-consumer situation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.