472,358 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Best way to implement a timed queue?

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

Similar topics

2
by: DuncanK (duncan (AT) mccarthy DOT co DOT nz) | last post by:
Hi there! I want to implement a 'queue' of records that are prioritised (development tasks in this case). I need to be able to 'move' the records up and down the queue to change their priority....
3
by: danavni | last post by:
i need to build a service that will accept incoming TCP/IP connections. the service should act like a "HUB" where on one side clients connect to it and stay connected for as long as they like and...
0
by: olsongt | last post by:
This one made me smile. From: http://aima.cs.berkeley.edu/python/utils.html#Queue class Queue: """Queue is an abstract class/interface. There are three types: Stack(): A Last In First Out...
6
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for...
5
by: SharpCoderMP | last post by:
is it possible to implement some safe way of performing two or more instructions in a kind of "atomic" way? consider this: we have two queues (implemented on top of an List<T>). now we need to...
10
by: John | last post by:
I want to write a code for Breadth First Traveral for Graph, which needs a queue to implement. I wonder that for such a powerful language as Python, whether there is a better and simpler...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
2
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include "Queuep.h" #include <cassert> #include <new> using namespace std; Queue::Queue () : backPtr (0), frontPtr(0)...
2
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include <cassert // for assert #include <new // for bad_alloc using namespace std; //private:{Queue::Queue(const...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.