Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4151

Python • Re: Wait for app to open with Python

$
0
0
Write pseudo code, once the logic of you pseudo code makes send, code it with actual python code.

Code:

keep_looking_for_audaciy = Truestop_looking_time = now + 20 secondsfound_audacity = falsewhile keep_looking_for_audaciy and now() < stop_looking_time:    if audacity is up:        try:    send command to itexcept:    command failedif command succeeded:            found_audacity = true            keep_looking_for_audaciy = false    else:          wait 2 secondsif found_audacity:    do whatever else your program needs to do nowelse:    did not find audacity after stop_looking_time or it did not successfully responded to the command, so print an error message and exit
The reason to keep the check of command succeeded outside of the try catch is that you may want the loop to go around until is succeeds. It may fail if audacity is up, but not yet to the point where it is accepting and answering commands.

Or you could check if it's there and if it is wait, say 5 seconds to give it enough time to start up properly, and then send the command. In this case you expect the command to just succeed, but if it does not, then just exit the loop by setting the keep_looking_for_audacity flag in the try block.

I'm sure there are a zillion different ways of doing this too.

I'd avoid your code start some other code when it finds audacity. Especially if you end up working with a python venv, because of that to work, you'll need to know about python venvs and why and how you use them, instead of just use them because someone told you to.
If you want me to have it wait to have Audacity boot up properly wouldn't I just be fine with the code I have then add a time.sleep(however long it takes to boot up)

Statistics: Posted by Henrik Gill — Mon Jul 22, 2024 7:49 pm



Viewing all articles
Browse latest Browse all 4151

Trending Articles