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

Best way to implement a timed queue?

P: n/a
Hello folks,

I am having troubles with implementing a timed queue. I am using the
'Queue' module to manage several queues. But I want a timed access, i.e.
only 2 fetches per second max. I am horribly stuck on even how I
actually could write it. Has somebody done that before? And when yes,
how is the best way to implement it?

Thanks,
Thomas
Jan 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Thomas Ploch wrote:
I am having troubles with implementing a timed queue. I am using
the 'Queue' module to manage several queues. But I want a timed
access, i.e. only 2 fetches per second max. I am horribly stuck on
even how I actually could write it. Has somebody done that before?
And when yes, how is the best way to implement it?
If you use an event loop system you could derive a class from your
queue class whose "pop" method only returns an element if some
timer has run out. After the maximum number of fetches you'd have
to reset the timer.

Regards,
Björn

--
BOFH excuse #327:

The POP server is out of Coke

Jan 4 '07 #2

P: n/a
Thomas Ploch schrieb:
I am having troubles with implementing a timed queue. I am using the
'Queue' module to manage several queues. But I want a timed access, i.e.
only 2 fetches per second max. I am horribly stuck on even how I
actually could write it. Has somebody done that before? And when yes,
how is the best way to implement it?
You could put a wrapper around the queue which synchronizes the get
operations, and then delays access until 1s after the last-but-one
access.

The following code is untested:

import threading, time

class ThrottledQueue(threading.Queue):
def __init__(self):
threading.Queue.__init__(self)
self.old = self.older = 0
self.get_lock = threading.Lock()

def get(self):
with self.get_lock: # synchronize get
# check whether the next get should be in the future
now = time.time()
next = self.older + 1
if now < next: time.sleep(next-now)
# really fetch one item; this may block
result = threading.Queue.get(self)
self.older = self.old
# set the last get time to the time when the get completed,
# not when it started
self.old = time.time()
return result

HTH,
Martin
Jan 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.