Re: How to make Python poll a PYTHON METHOD



Is it possible to call threads inside another thread (nested threads)?

The example above creates a thread to call a function "eat" every time
based on a specified interval.
Now for example, if I make the called function "eat" to spawn threads
to do the work in a queue and when all jobs are done, spawned threads
join. When the next interval is up this process repeat itself.

Thanks.

On May 7, 11:19 pm, Nick Vatamaniuc <vatam...@xxxxxxxxx> wrote:
On May 7, 10:42 pm, Nick Vatamaniuc <vatam...@xxxxxxxxx> wrote:



On May 7, 10:07 pm, johnny <rampet...@xxxxxxxxx> wrote:

Is there a way to call a function on a specified interval(seconds,
milliseconds) every time, like polling user defined method?

Thanks.

Sure,

def baz():

...: print "Baz!"
...:

from threading import Timer
timer=Timer(5.0,baz)
timer.start()
Baz!

Cheers,
-Nick Vatamaniuc

By the way, here is another way to do it. This way it will repeat, the
other one executed once. Of course, if it executed once, you can make
it do it again, but there is some trickery involved. Here is a way to
do it using threads.

Hope it helps,
-Nick Vatamaniuc

from threading import Thread
from time import sleep

class Repeater(Thread):
def __init__(self,interval,fun,*args,**kw):
Thread.__init__(self)
self.interval=interval
self.fun=fun
self.args=args
self.kw=kw
self.keep_going=True

def run(self):
while(self.keep_going):
sleep(self.interval)
self.fun(*self.args,**self.kw)

def stop_repeating(self):
self.keep_going=False

def eat(*a):
print "eating: " , ','.join([stuff for stuff in a])

r=Repeater(1.0, eat, 'eggs','spam','kelp')
r.start()
sleep(6.0)
r.stop_repeating()


.



Relevant Pages

  • Re: Inheritance Question
    ... Most animals walk the same way, but a few don't, so I create a subclass ... AW and override the walk method. ... Most animals eat the same way, but a few don't, so I create a subclass ... def walk: ...
    (comp.lang.python)
  • Re: Inheritance Question
    ... def general_method: pass ... Most animals walk the same way, but a few don't, so I create a subclass ... Most animals eat the same way, but a few don't, so I create a subclass ...
    (comp.lang.python)
  • Re: Alternate initializers or alternate class?
    ... class OrderHash ... def self.auto ... please have your cake and eat it too. ...
    (comp.lang.ruby)
  • Re: Alternate initializers or alternate class?
    ... > class OrderHash ... > def self.auto ... please have your cake and eat it too. ...
    (comp.lang.ruby)