0

What if I set two or more timers to call a function after the same amount of time. How can turtle's ontimer method handle this situation? Does it use multithreading or multiprocessing or none of them?

Gold_Leaf
  • 53
  • 7
  • The 'turtle' module is built upon `tkinter` so your question is closer related to `tkinter` as to `turtle`. Maybe you consider this fact rewriting your question and adding the `'tkinter' tag? – Claudio Jun 26 '22 at 13:00
  • https://stackoverflow.com/questions/48309112/python-tkinter-multithreading#48310386 : **Tkinter isn't designed to create widgets in one thread and then update them in another. – Bryan Oakley Jan 17, 2018** – Claudio Jun 26 '22 at 13:04
  • See here: https://stackoverflow.com/questions/1233963/how-operating-system-callbacks-work#1234050 for some background information. I suppose all applications and scripts in any language rely more or less on the same mechanism. – Claudio Jun 26 '22 at 14:23
  • I suggest you just write a piece of code to demonstrate you idea with multiple timers and then see what happens to the turtle action. There will be sure surprizing interferences between the timers and the timers and keyboard/mouse events ( what if you press two keys at the same time???). See here for code demonstrating this: https://stackoverflow.com/questions/72706669/unexpected-python-turtle-behavior-when-turning-was-invoked-from-keyboard – Claudio Jun 26 '22 at 14:27
  • By the way: at the level of turtle module you shouldn't worry about the behavior of the callback timer except after you face serious problems to get what you want to achieve. – Claudio Jun 26 '22 at 14:38
  • I’m voting to close this question because I can't see any effort towards the answer by the OP himself. – Claudio Jun 26 '22 at 14:42

1 Answers1

0

To demonstrate what generally happens if multiple timers are simultaneously acting I will use the example of the threading Timer which is probably also used by tkinter and turtle ( *this is an answer with a question ? to be answered maybe in the comments ).

from threading import Timer
def callback_Timer(timer_no, seconds):
    if seconds < 3: 
       print( "TimerNo:", timer_no, 'time:', seconds, 's' )
       seconds += 1
       Timer(5.0, lambda: callback_Timer(timer_no, seconds),[]).start()

seconds = 0
callback_Timer( 1, 0)
callback_Timer( 2, 0)
callback_Timer( 3, 0)
callback_Timer( 4, 0)
callback_Timer( 5, 0)

Gives following output:

TimerNo: 1 time: 0 s
TimerNo: 2 time: 0 s
TimerNo: 3 time: 0 s
TimerNo: 4 time: 0 s
TimerNo: 5 time: 0 s
TimerNo: 1 time: TimerNo:1 2 time: s 
1 s
TimerNo: TimerNo: 54  time:time:  11  ss
TimerNo: 
3 time: 1 s
TimerNo: 3 time: 2 s
TimerNo: 5 time: 2 s
TimerNo: 1 time: 2 s
TimerNo: 4 time: 2 s
TimerNo: 2 time: 2 s

as you can see: happens . No sequential order of the timers as at start but ... even more ... the print output of the Timer is disrupted by the other Timer.

It is up to YOU to handle issues related to a callback Timer ontimer() not the job of the turtle module.

The statement above is probably the best answer to your question:

What if I set two or more timers to call a function after the same amount of time. How can turtle's ontimer() method handle this situation?

Claudio
  • 7,474
  • 3
  • 18
  • 48