How to make python wait until the previous task is finished?

user3151828

Hi I am experimenting with Speech Synthesis on mac, and I always put while loops in my programs so that I can use them until I decide to stop, and with this code, it repeats "What would you like me to say?" At the same time it says whatever I tell it to say.

from Cocoa import NSSpeechSynthesizer
while 1==1
    sp = NSSpeechSynthesizer.alloc().initWithVoice_(None)
    sp.startSpeakingString_("What would you like me to say?")    
    say_1 = raw_input("What would you like me to say?")
    sp.startSpeakingString_(say_1)

Can someone tell me how to tell python to wait until it is done saying what I tell it to?

Paulo Bu

It seems you are looking for NSSpeechSynthesizer instance method: isSpeaking. You can write a polling loop to test if it is speaking and continue to work once it is not anymore. Something like this:

import time
from Cocoa import NSSpeechSynthesizer
while 1:
    sp = NSSpeechSynthesizer.alloc().initWithVoice_(None)
    sp.startSpeakingString_("What would you like me to say?")    
    say_1 = raw_input("What would you like me to say?")
    sp.startSpeakingString_(say_1)
    
    while sp.isSpeaking():    # loop until it finish to speak
        time.sleep(0.9)       # be nice with the CPU
    
    print 'done speaking'

UPDATE: Is better time.sleep than continue inside the loop. The latter will waste a lot of CPU and battery (as pointed out by @kindall).

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make python wait until the previous task is finished?

From Dev

How to make a python Script wait until download has finished

From Dev

How to make Gulp wait until dest file is finished?

From Dev

How do I make an Excel RefreshAll wait to close until finished?

From Dev

How to make JS wait until protocol execution finished

From Dev

Make Excel VBA wait until operation is finished

From Java

How to wait until all async calls are finished

From Dev

How to wait until an animation is finished in Swift?

From Dev

How to wait until all NSOperations is finished?

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

How to wait until some jQuery methods are finished?

From Dev

How to wait until networking by Alamofire has finished?

From Dev

How to wait until my batch file is finished

From Dev

How to get jQuery to wait until an animation is finished?

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

C# .Net - How to make application wait until all threads created in Library are finished

From Dev

Wait until function is finished

From Dev

Make slideToggle wait until other div's slideUps have finished?

From Dev

How to make future calls and wait until complete with Python?

From Dev

How can I make a function wait until a previous line of code is complete

From Dev

How to wait until all tasks are finished before running code

From Dev

How to wait until all tasks finished without blocking UI thread?

From Dev

In NodeJs how can I wait until my http get is finished?

From Dev

How can I wait until NSXMLParserDelegate methods finished?

From Dev

How to wait until an http request is finished before moving on?

From Dev

How to wait until canvas has finished re-rendering?

From Dev

Wait until forEach loop is finished?

From Dev

Wait until gobalEval has finished

From Dev

On click myFunction and wait until it is finished

Related Related

  1. 1

    How to make python wait until the previous task is finished?

  2. 2

    How to make a python Script wait until download has finished

  3. 3

    How to make Gulp wait until dest file is finished?

  4. 4

    How do I make an Excel RefreshAll wait to close until finished?

  5. 5

    How to make JS wait until protocol execution finished

  6. 6

    Make Excel VBA wait until operation is finished

  7. 7

    How to wait until all async calls are finished

  8. 8

    How to wait until an animation is finished in Swift?

  9. 9

    How to wait until all NSOperations is finished?

  10. 10

    how to wait until a web request with HttpWebRequest is finished?

  11. 11

    How to wait until some jQuery methods are finished?

  12. 12

    How to wait until networking by Alamofire has finished?

  13. 13

    How to wait until my batch file is finished

  14. 14

    How to get jQuery to wait until an animation is finished?

  15. 15

    Android - How to wait until a SnapshotListener has finished?

  16. 16

    C# .Net - How to make application wait until all threads created in Library are finished

  17. 17

    Wait until function is finished

  18. 18

    Make slideToggle wait until other div's slideUps have finished?

  19. 19

    How to make future calls and wait until complete with Python?

  20. 20

    How can I make a function wait until a previous line of code is complete

  21. 21

    How to wait until all tasks are finished before running code

  22. 22

    How to wait until all tasks finished without blocking UI thread?

  23. 23

    In NodeJs how can I wait until my http get is finished?

  24. 24

    How can I wait until NSXMLParserDelegate methods finished?

  25. 25

    How to wait until an http request is finished before moving on?

  26. 26

    How to wait until canvas has finished re-rendering?

  27. 27

    Wait until forEach loop is finished?

  28. 28

    Wait until gobalEval has finished

  29. 29

    On click myFunction and wait until it is finished

HotTag

Archive