473,651 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2563
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
2156
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. New tasks would added to the 'bottom' of the queue and completed records would disappear off the top. Any idea how to implement this in Access 97?
3
2090
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 on the other side the service reads messages for these clients from MSMQ and sends them to already connected clients. the clients can also send information back to the "HUB" which will be parsed and sent to MSMQ. when the clients connect to this...
0
1167
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 Queue. FIFOQueue(): A First In First Out Queue. PriorityQueue(lt): Queue where items are sorted by lt, (default <).
6
2859
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 example) or is doing some long calculations.
5
1694
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 move an element from one queue to another, so we need to do something like this: queue2.Enqueue(queue1.Dequeue()); int this case we may assume that nothing can cause this to fail resulting in loss of the object we want to move.
10
3787
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 implementation for a traditional FIFO queue? Thanks!
139
14136
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
2333
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
2926
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 Queue& Q)}
0
8352
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7297
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1587
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.