On 11/29/2011 10:52 AM, Mic wrote:
-----Ursprungligt meddelande----- From: Dave Angel
Sent: Tuesday, November 29, 2011 4:33 PM
<SNIP>
-----Original Message-----
From: "Mic"<o0m...@hotmail.se>
Sender: tutor-bounces+bodsda=googlemail....@python.org
Date: Tue, 29 Nov 2011 15:54:59
To:<tutor@python.org>
Subject: [Tutor] Making a function run every second.
Hi
I want a function to run every second , how do I do that?
Say that the function look like this:
def hi():
print("hi")
Without a clearer spec, there are too many possible answers. As Bobsda
says, you can't get it exactly one second apart. But you also have the
problem of whether a drift is okay. For example, if you have a global
you're incrementing each time that function runs, and you want it to
represent the total time the program has been running, then a simple
sleep() is totally wrong.
Okay, I undestand. Hmm, what is a drift?
What I was concerned about is that perhaps this is for one of the
tkinter programs Mic is writing. For an event-driven program, sleep()
calls of even a second are unaccepable. And if you literally write a
while loop like that, your whole program would stop responding.
Yes, this is one of my tkinter programs :) Why would the program stop
responding using a sleep()?
I'm not that familiar with tkinter details. But like all (almost??)
GUI's it relies on an event loop. Each event has to be "quick" for some
definition of quick. In the case of tkinter, I believe that eventloop
is called mainloop(). That loop won't return till you hit the X button
in the corner, or do something else that triggers program cancellation.
That loop is the thing that interacts with the OS, and calls various
event handlers as things happen. If you were to do a loop of sleep()
before you got to the mainloop() call, the GUI would never start. If
you do it inside some event, then the mainloop() never gets control again.
tkinter provides a couple of specific timer events, and I now see your
reply to a message that said to use the after() method, which is a
one-shot. I believe there's another one that sets a periodic timer so
you don't have to do an after() call each time it fires.
So Mik:
Tell us more about the real requirements. Is drift acceptable, is this
a console program or some gui environment, are there any other hidden
assumptions?
Okay, this function approximately runs every second and check what the
time is.
(Well, every 30 second or every minute would be okay if that makes it
easier)
If the time is 15:00 it is supposed to remove a file. That should be all
requirements!
I have figured out how to make the function check what time it is and
how to
remove the file when the time is 15:00, but I don't know how to make it
run every
second or something like that.
Thanks!
Mic
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor