0

Right, I'm quite new and I've been looking around for an answer but I just can't seem to find one that fits me.

This question is pretty much the same as mine, but I need to execute a function every... Let's say 1.2 seconds and without interrupting or blocking the whole while loop.

The reason I won't use modules (except for time) is because the only available ones to me are: builtins, math, matplotlib.pyplot, numpy, operator, processing, pygal, random, re, string, time, turtle and urllib.request.

Is there such a way?

4
  • 1
    Why don't you have the whole standard library? Commented May 7, 2017 at 19:11
  • Use a while loop in a thread
    – gaborous
    Commented May 7, 2017 at 19:37
  • I'm tempted to say 'no' unless you've missed out threading (or something with similar capabilities) from your list.
    – Bill Bell
    Commented May 7, 2017 at 20:12
  • It's because I'm programming through such a site called trinket.io.
    – MrWhiteee
    Commented May 8, 2017 at 14:42

2 Answers 2

1

If you have turtle available, then you have tkinter available as turtle.TK. You can then use root.after(1200, function. args) to execute function(*args) every 1.2 seconds. Searching SO for [tkinter] root.after will give numerous questions with helpful examples. However, once you do that, you must make everything event driven and event handlers should not take so long as to block the event loop.

EDIT: turtle wraps tkinter.after as turtle.ontimer(function, milleseconds). The function cannot take arguments. If this is a 'homework' problem of some sort, this may be the intended solution. There is an example here.

1
  • Thanks, that'll help me a ton.
    – MrWhiteee
    Commented May 8, 2017 at 14:40
0

Use the module time. You have it on your list.

import time
def function():
 print(1)
while True:
 time.sleep(1.2)
 function()
1
  • sleep blocks the while loop Commented May 7, 2017 at 20:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.