473,765 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Producer/consumer Queue "trick"

WEBoggle needs a new game board every three minutes. Boards take an
unpredictable (much less than 3min, but non-trivial) amount of time to
generate. The system is driven by web requests, and I don't want the
request that happens to trigger the need for the new board to have to
pay the time cost of generating it.

I set up a producer thread that does nothing but generate boards and put
them into a length-two Queue (blocking). At the rate that boards are
pulled from the Queue, it's almost always full, but my poor consumer
thread was still being blocked for "a long time" each time it fetched a
board.

At this point I realized that q.get() on a full Queue immediately wakes
up the producer, which has been blocked waiting to add a board to the
Queue. It sets about generating the next board, and the consumer
doesn't get to run again until the producer blocks again or is preempted.

The solution was simple: have the producer time.sleep(0.00 1) when
q.put(board) returns.

Cheers,

Evan @ 4-am

Jul 18 '05 #1
7 2371
I don't get it.

If the consumer and the producer are separate threads,
why does the consumer thread block when the producer
thread is generating a new board? Or why does it
take forever for the producer thread to be pre-empted?

Also, I don't understand why the solution works.
How does sleeping for .001 seconds right after putting
a new board on the queue solve the problem?

Evan Simpson wrote:
WEBoggle needs a new game board every three minutes. Boards take an
unpredictable (much less than 3min, but non-trivial) amount of time to
generate. The system is driven by web requests, and I don't want the
request that happens to trigger the need for the new board to have to
pay the time cost of generating it.

I set up a producer thread that does nothing but generate boards and put
them into a length-two Queue (blocking). At the rate that boards are
pulled from the Queue, it's almost always full, but my poor consumer
thread was still being blocked for "a long time" each time it fetched a
board.

At this point I realized that q.get() on a full Queue immediately wakes
up the producer, which has been blocked waiting to add a board to the
Queue. It sets about generating the next board, and the consumer
doesn't get to run again until the producer blocks again or is preempted.

The solution was simple: have the producer time.sleep(0.00 1) when
q.put(board) returns.

Jul 18 '05 #2
Evan Simpson <ev**@tokenexch ange.com> writes:
wakes up the producer, which has been blocked waiting to add a board
to the Queue. It sets about generating the next board, and the
consumer doesn't get to run again until the producer blocks again or
is preempted.


That's weird. Preemption should happen every few dozen milliseconds
unless you've purposely increased the preemption delay.
Jul 18 '05 #3
Paul Rubin wrote:
Evan Simpson <ev**@tokenexch ange.com> writes:
wakes up the producer, which has been blocked waiting to add a board
to the Queue. It sets about generating the next board, and the
consumer doesn't get to run again until the producer blocks again or
is preempted.

That's weird. Preemption should happen every few dozen milliseconds
unless you've purposely increased the preemption delay.


To me, it smells like a call into a C extension which isn't releasing the GIL
before starting a time-consuming operation. After getting bitten by this a
couple of times, I now make sure to release the GIL as part of my SWIG wrapper
(since the code I'm wrapping knows nothing of Python, and sure as heck doesn't
need to be holding the GIL!).

Cheers,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #4
On Fri, 14 Jan 2005 16:26:02 -0600, Evan Simpson wrote:
WEBoggle needs a new game board every three minutes. Boards take an
unpredictable (much less than 3min, but non-trivial) amount of time to
generate.


I gotta ask, why?

Looking over your information about "how to play", my only guess is that
you're generating all possible words that may exist in the board, at the
time of board generation.

But it also looks like you score once at the end (as you have to anyhow in
order to cancel out words found by multiple people, according to the rules
of Boggle).

If both of these statements are true, the generation of all possible words
is a waste of time. For each word given by a human, looking it up in the
dict and verifying it is on the board at score time should be a lot
faster, and not need any pre-processing.

If you're generating stats about what percentage of possible words were
found, that can also be done during game play without loss by handing
outh the board, and *then* finding all words. You still have a threading
problem, but now instead of dealing with human response times, you've got
a three minute deadline which ought to be enough.

(The other thing I can think of is that you are trying to verify that the
board contains some minimal number of words, in which case I submit that
boards with only 20-ish words is just part of the game :-) I've never sat
down and really studied the Boggle dice, but I've always expected/hoped
that there is at least one or two dice with all vowels; even so the odds
of no vowels are small and easily algorithmically discarded. )

To be clear, I'm mostly curious what's going on, but it is possible that
the fundamental problem may be algorithmic, and since I don't know what's
going on it's worth checking.

Also, entirely separate plug, you may be interested in my XBLinJS project:
http://www.jerf.org/resources/xblinjs . (I'm expecting to release 0.2
either today or tomorrow, which will have vastly more documentation and
more online examples.) It'd help pull out relevant code so that it is
easily re-usable in any future gaming projects by others.
Jul 18 '05 #5
I should clarify up front that I may have given an overblown sense of
how long the producer thread typically takes to generate a board; It's
usually a few tenths of a second, up to a few seconds for especially
fecund boards.

My concern was that even a few seconds is long enough for fifty requests
to get piled up, and I was experiencing mysterious breakdowns where
Apache was suddenly totally clogged up and taking *minutes* to respond.

Jeremy Bowers wrote:
Looking over your information about "how to play", my only guess is that
you're generating all possible words that may exist in the board, at the
time of board generation.
Yep. I do this in order to minimize the cost of the most common request
that WEBoggle handles, which is checking a submitted word to see whether
it is a valid word on the board, a valid word not on the board, or an
invalid word. With the current code, this averages 1ms.
But it also looks like you score once at the end (as you have to anyhow in
order to cancel out words found by multiple people, according to the rules
of Boggle).
WEBoggle is a little different than regular Boggle, in that your score
is the plain sum of the scores for all of the words that you found, with
no cancellation. I'm planning to add a "vs." feature eventually that
will involve cancellation, but even then I'll retain the immediate
feedback upon guessing a word.

In addition, many players enjoy seeing the list of "Words not found by
anyone".
(The other thing I can think of is that you are trying to verify that the
board contains some minimal number of words, in which case I submit that
boards with only 20-ish words is just part of the game :-) I've never sat
down and really studied the Boggle dice, but I've always expected/hoped
that there is at least one or two dice with all vowels; even so the odds
of no vowels are small and easily algorithmically discarded. )
Believe it or not, before I added code to filter them out, I was
generating enough boards with *zero* valid words on them to get complaints.
Also, entirely separate plug, you may be interested in my XBLinJS project


Very nifty! Well beyond my current needs, but good to know about.

Cheers,

Evan @ 4-am

Jul 18 '05 #6
Jon Perez wrote:
If the consumer and the producer are separate threads,
why does the consumer thread block when the producer
thread is generating a new board? Or why does it
take forever for the producer thread to be pre-empted?

Also, I don't understand why the solution works.
How does sleeping for .001 seconds right after putting
a new board on the queue solve the problem?


I'm guessing at how things work, and may be totally wrong, but here's
what I think happens: In the Queue get() code, the consumer releases
the 'fsema' lock. Directly or indirectly, this wakes up and hands
control to the producer thread, which was blocked trying to acquire
'fsema'. The sleep() hands control back to the scheduler immediately,
which appears to wake up the consumer and let it get on with things.

It doesn't take "forever" for the producer to be preempted, just the
normal preemption interval. I was bothered, though, by the idea of
having it take even a few dozen milliseconds out of the middle of a
request that's at most a millisecond or two away from finishing anyway.

Cheers,

Evan @ 4-am

Jul 18 '05 #7
Nick Coghlan wrote:
Paul Rubin wrote:
That's weird. Preemption should happen every few dozen milliseconds
unless you've purposely increased the preemption delay.

To me, it smells like a call into a C extension which isn't releasing
the GIL before starting a time-consuming operation.


Sorry, I think I misled you both -- "a long time" is actually the few
dozen milliseconds of a normal preemption, which is only long relative
to the single millisecond that the consumer typically takes to handle a
request.

Cheers,

Evan @ 4-am

Jul 18 '05 #8

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

Similar topics

6
2243
by: Wonjae Lee | last post by:
I read the comment of http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277753. (Title : Find and replace string in all files in a directory) "perl -p -i -e 's/change this/..to this/g'" trick looks handy. Does Python have a similar trick? Or, is there a shorter Python recipe for the given problem?
2
2405
by: Michael Lehn | last post by:
I want to use the "Barton Nackman trick" for my matrix hierarchy. But I ran into a problem with a typedef I need in the base class: template <typename E> class Matrix { public: typedef typename E::TT TT; };
10
54600
by: qazmlp | last post by:
There are some blocks of C/C++ code put under #if 0 #end if Is there anyway to make the code inside these blocks to get executed (may be by using some command line options)?
13
3998
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate button "Unexpected Error":3251 operation is not supported for this type of object.The demo cd has two databases, one is called inventory and the other just has the tables for the design called inventory data. When you run inventory the database works...
20
4110
by: Tony | last post by:
I have a situation where I want to send data, but I have no need for a response. It seems to me that XMLHTTPRequest is the best way to send the data, but I don't need any response back from the server. Basically, I'm writing js errors to an error log on the server side - and there is no need to inform the user that the error has been logged. The problem is that I don't want to sit with the request open & waiting for a response, when I...
7
16361
by: PW | last post by:
Hi, I have a form with unbound fields on it. The user selects a record from a recordset and I populate the unbound fields. When I try to change the unbound quantity text box, Access 2003 tells me "The data has been changed. Another user edited this record and saved the changes before you attempted to save your changes. Re-edit the record." This does not always happen. And yes, I can re-edit the quantity text
21
7858
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
6
1813
by: =?ISO-8859-1?Q?Une_B=E9vue?= | last post by:
i'd like to intercept the window.onload event in order to distribute it, as needed, to several methods. example : suppose i have several methods doing unlinked initialisations: method_1=function(e){<initializes variable 1>} .... method_n=function(e){<initializes variable n>}
56
6765
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under 6.4.2(2) : case constant-expression : I propose that the case expression of the switch statement be changed from "integral constant-expression" to "integral expression".
0
9568
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
9398
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
10007
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
9951
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
8831
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
6649
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
5275
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
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.