472,135 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1718
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by chrisdevey | last post: by
2 posts views Thread by Michael Turner | last post: by
5 posts views Thread by Mark | last post: by
8 posts views Thread by =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post: by
5 posts views Thread by lucius | last post: by
4 posts views Thread by John Dann | last post: by
reply views Thread by Rob Weir | last post: by
reply views Thread by leo001 | last post: by

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.