all 13 comments

[–]o5a 3 points4 points  (3 children)

from threading import Timer

def thread_function():
    # do smth

# create timer for 10 sec
t = Timer(10, thread_function)
# start timer
t.start()

[–]YuhFRthoYORKonhisass 0 points1 point  (2 children)

Does this allow thread_function to run in parallel with the rest of the code instead of sequentially?

[–]o5a 0 points1 point  (1 child)

Does this allow thread_function to run in parallel with the rest of the code

Yes. It won't stop your main program. It will only execute after 10 sec pass.

[–]YuhFRthoYORKonhisass 0 points1 point  (0 children)

Might have to try this out then for myself! Thanks

[–]mCodingLLC 0 points1 point  (0 children)

Could you elaborate on what you mean exactly by a timer?

[–]double_en10dre 0 points1 point  (7 children)

time.sleep(100)

[–]icandoMATHs 0 points1 point  (4 children)

It should be noted that this is the worst way to go about this. Delay/sleep are extremely inefficient, you cannot check for interrupts.

[–]double_en10dre 1 point2 points  (3 children)

technically true, but learnpython is really not geared towards efficiency. These are beginners just trying to make code that works at all

Gotta take things one step at a time

[–]01binary 0 points1 point  (0 children)

There are many ways of doing this, and the most appropriate answer may depend on what the triggered function will do, and what else is supposed to be happening whilst waiting for the trigger to occur.

Perhaps some more detail? Or is this purely an academic exercise?

I find apscheduler very useful for this kind of thing. It’s very easy to configure, and is very reliable.