473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to make Python poll a PYTHON METHOD

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
5 7251
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Achim Domma | last post by:
Hello, I need mysql-python for python 2.3 on windows. I downloaded the source and tried to build it myself, but I get linker errors: mysqlclient.lib(password.obj) : error LNK2001: unresolved...
23
by: Robey Holderith | last post by:
Anyone know a good way to embed python within python? Now before you tell me that's silly, let me explain what I'd like to do. I'd like to allow user-defined scriptable objects. I'd like to...
8
by: Maurice LING | last post by:
Hi, anyone had any experiences in embedding python in python? I've tried to do this but it doesn't work. eval("from Tkinter import *") Thanks maurice
4
by: Mario | last post by:
Hello everybody, Does anybody know how i make a script to make a poll? I mean a small poll when you can choose yes or no? -- -------"""------- ---()--- °?----(_)-----?° | Greetz Mario|
10
by: Michael Hoffman | last post by:
Does anyone have a script to convert more conventional USENET quoting style like this: John wrote: > Jacob Jingleheimer Schmidt <jjs@example.net> wrote in message-id <gratuitous-detail> on 29...
4
by: stefano | last post by:
I need make some images using python but i'm lost :P i need some module to make .png (with drawline, drawcircle, drawpoint etc etc etc ) like gd for php :P thants :D
1
by: Davy | last post by:
Hi all, How to make a standalone Python/Tk program(e.g. exe file on Windows)? Any suggestions are welcome! Best regards, Davy
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.