1

Question

I need to be able to call a function in the background without it freezing the console. I have experience with multithreading, but I would prefer if it completed tasks in order. What's the best way to do this? Example code is greatly appreciated as English isn't my first language.

Background information (Specific to my question)

I'm using a heavily modified version of pyttsx, thus when a specific function is called it performs a SAPI call which freezes up the console. I would like to be able to call speak.main(decrypt(data)) and still be able to continue inputting data whilst my computer is speaking.

Community
  • 1
  • 1
Jamus
  • 865
  • 4
  • 11
  • 28
  • 1
    Have two threads, a speaking thread and a console thread. Make a queue shared between the two and when new data needs to be spoken, shove it on the queue. The speaking thread idles it the queue is empty, if not, it pops a value and speaks. – Gareth Latty Jun 04 '13 at 12:20
  • Thank you! If you'd like to put this as a question, I'd be happy to mark it. You solved my issue! :) http://www.lonelycode.com/2011/02/04/python-threading-and-queues-and-why-its-awesome/ – Jamus Jun 04 '13 at 12:45
  • possible duplicate of [background function in Python](http://stackoverflow.com/questions/7168508/background-function-in-python) – taocp Jun 04 '13 at 13:42

1 Answers1

1

My suggestion would be to have two threads, a speaking thread and a console thread. Make a queue shared between the two and when new data needs to be spoken, shove it on the queue. The speaking thread idles it the queue is empty, if not, it pops a value and speaks.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183