On 14/11/12 02:42, Patrick Dickey wrote:
Hi everyone,

I've got an application that will use a timer to run a function
automatically (it's an update function for my IPv6 endpoint). The
questions that I have are these:

Have you read the documentation?

http://docs.python.org/2/library/threading.html#timer-objects

1.  Can I stop and start the timer from different functions or methods
in my program, and if so, how?

As the docs say:

[quote]
Timers are started, as with threads, by calling their start() method.
The timer can be stopped (before its action has begun) by calling
the cancel() method.
[end quote]

Any function or method that has access to the timer object can call
the start or cancel method. You just need to make sure that the
function can see the timer object, using one of the standard ways
in Python of giving a function access to any other object:

* (best) pass the object into the function as an argument

  result = myfunction(a, b, c, some_timer)

* (easiest) make the timer a global variable

* put the timer inside a dict, list or other container and
  pass it to the function

* put the timer in an attribute of the object

etc.



2.  Can I stop the timer, change the value, and restart it (or would it
create a new timer), or do I have to create a new timer with an entirely
new name?

Changing the timer is not supported. I expect that calling my_timer.start()
after cancelling it would restart it, but haven't tested it.

Just because you create a new timer doesn't mean you have to give it an
entirely new name.


3.  If I get a value from a textbox, how do I parse it from the string
value to an integer (or float)?

This has nothing to do with timers, and should go into a separate email
thread so that those people who know nothing about threading can contribute.

Regardless of where the string comes from, you turn a string into an int or
float by calling the int() or float() function.


4.  Is there a better way of accomplishing this task?

Here's the pseudocode for what I'm doing.

if autoUpdates is enabled
    get updateFrequency
    start timer with time value from updateFrequency
    when time is reached, run update method
else
    cancel timer


Well that won't work, because once the update has run, it will stop
checking for new updates.


if autoUpdates is enabled AND user changes updateFrequency
    stop timer
    get updateFrequency
    start timer with new time value from updateFrequency

Seems fine to me.



--
Steven
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to