I am figuring out how to have my python try to complete an action ( that may never be able to happen ) until what is the equivalent of a timer runs out in which case it runs a separate function.
The exact scenario is bypassing the "Warning" screen that outlook provides when something of an automation system tries accessing it. When the initial command is sent to retrieve data from or otherwise manipulate outlook the entire python script just stops and waits ( as best as I can tell ) waiting for the user to click "Allow" on the outlook program before it will continue. What I'd like to happen is that upon it trying to do the manipulation of outlook there be a timer that starts. If the timer reaches X value, skip that command that was sent to outlook and do a different set of actions.
I feel that this may lead into something called "Threading" in order to have simultaneous processes running but I also feel that I may be over complicating the concept. If I were to do a mockup of what I think may be written to accomplish this, this is what I'd come up with...
time1 = time.clock()
try:
mail = inbox.Items[0].Sender
except if time1 > time1+10:
outlookWarningFunc()
I am 99.9% sure that "except" isn't ever used in such a manner hence why it isn't a functional piece of code but it was the closest thing I could think of to at least convey what I am trying to get to.
I appreciate your time. Thank you.
One of the solutions is this:
import threading
mail = None
def func():
global mail
mail = inbox.Items[0].Sender
thread = threading.Thread(target=func)
thread.start()
thread.join(timeout=10)
if thread.is_alive():
# operation not completed
outlookWarningFunc()
# you must do cleanup here and stop the thread
You start a new thread which performs the operation and wait 10 seconds for it until it completes or the time is out. Then, you check if job is done. If the thread is alive, it means that the task was not completed yet.
The pitfall of this solution is that the thread is still running in the background, so you must do cleanup actions which allows the thread to complete or raise an exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With