Re: How to run a repeating timer every n minutes?

2009-10-30 Thread Frank Millman
Diez B. Roggisch wrote:
 mk wrote:


 I'm newbie at threading, so I'm actually asking: should not method like
 stop() be surrounded with acquire() and release() of some threading.lock?

 I mean, is this safe to update running thread's data from the main
 thread without lock?

 stop() is part of the Timer-interface, and tas it's not mentioned to be
 unsafe in the docs you can just call it. It might be that it internally
 calls some threadsafe means of communication (Event, Lock).


Thought I should jump in here to avoid any confusion.

mk is referring to the Timer class that I included in my reply to the OP. It 
is different from the Timer class in the threading module. I had not 
realised that I was duplicating an existing name - sorry about that.

The standard Timer class has a method called cancel(). I am sure that Diez's 
comments above will apply to this.

I included a method called stop() in my Timer class, and I think it is to 
this that mk is referring.

The method looks like this -

def stop(self):
self.event.set()

Is this threadsafe? I would have thought the answer is yes, but I am no 
expert. Perhaps someone else can confirm.

Thanks

Frank Millman



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a repeating timer every n minutes?

2009-10-29 Thread Wesley Brooks
I use the wx.Timer for this:

import wx

timer = wx.Timer(self, -1)
# update gui every 1/4 second (250ms)
timer.Start(250)
Bind(wx.EVT_TIMER, OnUpdateValues)

In the above I'm running the OnUpdateValues function every 250ms.

Regards,

Wesley Brooks


2009/10/29 VYAS ASHISH M-NTB837 ashish.v...@motorola.com


 Dear All

 How do I write a code that gets executed 'every x' minutes?



 I know how to do it 'after x' minutes, I do the following:

 def doAtTimerFire():
 The things I want to do 'after x' minutes go here. 

 And then from main code, I do this:

 tmr = threading.Timer(timeInSeconds, doAtTimerFire)
 tmr.start()


 Sorry about the earlier post with wrong subject line.
 Please help.

 Regards,
 Ashish Vyas

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a repeating timer every n minutes?

2009-10-29 Thread Martin P. Hellwig

VYAS ASHISH M-NTB837 wrote:
cut
You might want to start a thread with a continues loop that primarily 
sleeps (time.sleep) but wakes up at regular intervals and executes what 
needs to be.


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a repeating timer every n minutes?

2009-10-29 Thread Frank Millman
Ashish Vyas wrote:

 Dear All

 How do I write a code that gets executed 'every x' minutes?


[...]

 Regards,
 Ashish Vyas

Here is one way -

import threading

class Timer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()

def run(self):
while not self.event.is_set():
  The things I want to do go here. 
 self.event.wait(number_of_seconds_to_wait)

def stop(self):
self.event.set()

In your main program -
  - to start the timer
  tmr = Timer()
  tmr.start()

  - to stop the timer
  tmr.stop()

It is easy to extend this by passing the number_of_seconds_to_wait, or a 
function name to be executed, as arguments to the Timer.

Frank Millman



-- 
http://mail.python.org/mailman/listinfo/python-list


RE: How to run a repeating timer every n minutes?

2009-10-29 Thread VYAS ASHISH M-NTB837
 
Thanks a lot, this helps.

-Original Message-
From: python-list-bounces+ntb837=motorola@python.org
[mailto:python-list-bounces+ntb837=motorola@python.org] On Behalf Of
Frank Millman
Sent: Thursday, October 29, 2009 5:19 PM
To: python-list@python.org
Subject: Re: How to run a repeating timer every n minutes?

Ashish Vyas wrote:

 Dear All

 How do I write a code that gets executed 'every x' minutes?


[...]

 Regards,
 Ashish Vyas

Here is one way -

import threading

class Timer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()

def run(self):
while not self.event.is_set():
  The things I want to do go here. 
 self.event.wait(number_of_seconds_to_wait)

def stop(self):
self.event.set()

In your main program -
  - to start the timer
  tmr = Timer()
  tmr.start()

  - to stop the timer
  tmr.stop()

It is easy to extend this by passing the number_of_seconds_to_wait, or a
function name to be executed, as arguments to the Timer.

Frank Millman



--
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a repeating timer every n minutes?

2009-10-29 Thread mk

Frank Millman wrote:


class Timer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()

def run(self):
while not self.event.is_set():
  The things I want to do go here. 
 self.event.wait(number_of_seconds_to_wait)

def stop(self):
self.event.set()

In your main program -
  - to start the timer
  tmr = Timer()
  tmr.start()

  - to stop the timer
  tmr.stop()

It is easy to extend this by passing the number_of_seconds_to_wait, or a 
function name to be executed, as arguments to the Timer.


I'm newbie at threading, so I'm actually asking: should not method like 
stop() be surrounded with acquire() and release() of some threading.lock?


I mean, is this safe to update running thread's data from the main 
thread without lock?


Regards,
mk


--
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a repeating timer every n minutes?

2009-10-29 Thread Diez B. Roggisch
mk wrote:

 Frank Millman wrote:
 
 class Timer(threading.Thread):
 def __init__(self):
 threading.Thread.__init__(self)
 self.event = threading.Event()
 
 def run(self):
 while not self.event.is_set():
   The things I want to do go here. 
  self.event.wait(number_of_seconds_to_wait)
 
 def stop(self):
 self.event.set()
 
 In your main program -
   - to start the timer
   tmr = Timer()
   tmr.start()
 
   - to stop the timer
   tmr.stop()
 
 It is easy to extend this by passing the number_of_seconds_to_wait, or a
 function name to be executed, as arguments to the Timer.
 
 I'm newbie at threading, so I'm actually asking: should not method like
 stop() be surrounded with acquire() and release() of some threading.lock?
 
 I mean, is this safe to update running thread's data from the main
 thread without lock?

stop() is part of the Timer-interface, and tas it's not mentioned to be
unsafe in the docs you can just call it. It might be that it internally
calls some threadsafe means of communication (Event, Lock).

In general you are right, however many of those considerations don't apply
to (C)Python due to the GIL (global interpreter lock), which ensures that a
lot of operations are atomic. For example, in current CPython it's
perfectly safe to simply set a boolean instance variable to False to stop
an running loop.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list