473,509 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Timer or Scheduler in a Class

I'd like a class method to fire every n seconds.

I tried this:

class Timed:
def.__init__(self):
self.t = Timer(3, self.dothing)

def.start(self):
self.t.start()

def.dothing(self):
print "Doing Thing"

s = new Timed()
s.start()

And:

class Scheduled:
def.__init__(self):
self.s = sched.scheduler(time.time, time.sleep)
self.s.enter(3, 1, self.sync, ())

def.start(self):
self.t.start()

def.dothing(self):
print "Syncing"

s = new Scheduled()
s.start()

Both run once and end. I'm obviously missing something here.

Thanks,

Justin
Aug 14 '08 #1
1 1798
Hi Justin,

Does Professor Battersea know you're using his gmail account? *wink*
On Wed, 13 Aug 2008 23:16:12 -0400, Prof. William Battersea wrote:
I'd like a class method to fire every n seconds.

I tried this:

class Timed:
def.__init__(self):
self.t = Timer(3, self.dothing)
def.start(self):
self.t.start()

def.dothing(self):
print "Doing Thing"

s = new Timed()
s.start()
This can't be your actual code, because "s = new Timed()" gives a
SyntaxError. So does "def.start(self)" etc.

Also, what's Timer?

And:

class Scheduled:
def.__init__(self):
self.s = sched.scheduler(time.time, time.sleep)
self.s.enter(3, 1, self.sync, ())

def.start(self):
self.t.start()

def.dothing(self):
print "Syncing"

s = new Scheduled()
s.start()
When I fix the syntax errors and try to run the above code, I get this:

AttributeError: Scheduled instance has no attribute 'sync'

That's only the first of a number of errors. You waste our time when you
post code that doesn't run. Very few people will bother spending the time
and effort to fix your code if you don't respect their time, and those
that do will rub your nose in the fact that you're wasting their time.

Both run once and end. I'm obviously missing something here.
Let's start with some code that actually does run:
>>class Scheduled:
.... def __init__(self):
.... self.s = sched.scheduler(time.time, time.sleep)
.... self.s.enter(3, 1, self.dothing, ())
.... def start(self):
.... self.s.run()
.... self.s.enter(3, 1, self.dothing, ())
.... self.start()
.... def dothing(self):
.... print "Syncing"
....
>>s = Scheduled()
s.start()
Syncing
Syncing
Syncing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in start
File "<stdin>", line 8, in start
File "<stdin>", line 8, in start
File "<stdin>", line 6, in start
File "/usr/lib/python2.5/sched.py", line 108, in run
delayfunc(time - now)
KeyboardInterrupt

This will run until you interrupt it (as I did) or you run out of space
on the stack due to recursion. I imagine this is probably not the best
way to do what you want.

Hint for further explorations: the scheduler keeps a queue of events. If
the queue becomes empty, it stops. You only need to restart the scheduler
with run() if it stopped, otherwise it keeps going.

--
Steven
Aug 14 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3240
by: chrisdevey | last post by:
Is there any way to make a System.Timers.Timer adjust for daylight savings time change? In a long running process I set a timer as follows for a daily expiration: _myTimer = new...
18
3229
by: Max | last post by:
This is a follow-up on my previous thread concerning having the program wait for a certain date and time and then executing some code when it gets there. My question is; can I use the Sleep...
2
1668
by: Michael Turner | last post by:
Ho can I set a timer to run a set routine at a set time on a set day of the week. Mike
5
1139
by: Mark | last post by:
Hello, Does anyone know of a timer control which has similar functionality as the scheduler in SQL server? I'm most interested in being able to set up recurring tasks. I want to stay away...
8
2636
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
5
1422
by: lucius | last post by:
With ASP.NET/Framework 1.1, I would like to have a class that instances on application startup, and every 60 seconds after that does some work (actually a database fetch). Then any .aspx/.asmx page...
10
3048
by: Zytan | last post by:
I made a program using the Timer class, and I start the timer with Timer.Start(), but don't stop it with Timer.Stop(), and I assumed this was ok, but, Process Explorer informs me, after running it...
4
3945
by: John Dann | last post by:
I need what I'd call (in .Net) a timer, ie I need to run a function eg every 2 seconds - it doesn't need to be millisec accurate but it would be nice if it wasn't eg every 4 seconds or something. ...
0
860
by: Rob Weir | last post by:
On 14 Aug 2008, William Battersea wrote: That's how both ('sched' and threading.Timer) of the them work. Depending on what you're doing, your toolkit/framework (Twisted, GTK, etc) might have a...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
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
7416
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...
1
7073
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
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.