简体   繁体   中英

Way to repeatedly execute a function without extra modules every x seconds?

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?

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 .

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

import time
def function():
 print(1)
while True:
 time.sleep(1.2)
 function()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM