By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

How to make Python poll a PYTHON METHOD

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

Thanks.

May 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On May 7, 10:07 pm, johnny <rampet...@gmail.comwrote:
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

May 8 '07 #2

P: n/a
johnny wrote:
Is there a way to call a function on a specified interval(seconds,
milliseconds) every time, like polling user defined method?

Thanks.
A very literal interpretation of your question yields the following very
simple answer:

import time
while True:
time.sleep(some_number_of_seconds)
user_defined_method()
A more sophisticated approach would use threading, for example:

http://aspn.activestate.com/ASPN/Coo.../Recipe/464959

James
May 8 '07 #3

P: n/a
On May 7, 10:42 pm, Nick Vatamaniuc <vatam...@gmail.comwrote:
On May 7, 10:07 pm, johnny <rampet...@gmail.comwrote:
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()

May 8 '07 #4

P: n/a
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...@gmail.comwrote:
On May 7, 10:42 pm, Nick Vatamaniuc <vatam...@gmail.comwrote:
On May 7, 10:07 pm, johnny <rampet...@gmail.comwrote:
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()

May 10 '07 #5

P: n/a
On 2007-05-10, johnny <ra*******@gmail.comwrote:
Is it possible to call threads inside another thread (nested threads)?
No. It's not possible to call threads because they're not
callable objects. (I'm assuming you're talking about Thread
objects from the threading module.)

If you're asking if you can create and start threads from a
non-main thread, the answer is yes.
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.
OK.

--
Grant Edwards grante Yow! I demand IMPUNITY!
at
visi.com
May 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.