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")
Akash Nil
  • 693
  • 10
  • 26
  • 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. – Ken Cheung Nov 12 '15 at 08:19
  • Sir I am new in threading if you kindly provide me an example it will be very helpful for me. – Akash Nil Nov 12 '15 at 08: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? – Pynchia Nov 12 '15 at 08:40

1 Answers1

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

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • please follow [PEP-8 guidelines](https://www.python.org/dev/peps/pep-0008/) for naming conventions. – Pynchia Nov 12 '15 at 08:32
  • Where is `receiveddata` defined/assigned? – Pynchia Nov 12 '15 at 08:34
  • @Pynchia `data = xbee.readline()`, i simply renamed the `recieveddata` to `data` and passed that to the class (that will become a thread). – Torxed Nov 12 '15 at 08:37
  • @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. – Torxed Nov 12 '15 at 08:41
  • 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. – Pynchia Nov 12 '15 at 08:46
  • @Pynchia Absolutely, creating a "Living thread" with a neat function to call for the ammouncement would be ideal, but then you'd need a thread govenor that terminates the thread accordingly and some other things. This is the most basic framework I could come up with that wasn't a massive PITA to take in for a first-timer as the user explained himself to be. I couldn't make the code easier to understand without him asking more questions (considering you even asked where `recieveddata` is defined when it should be obvious told me that this code was enough to begin with). – Torxed Nov 12 '15 at 08:55