473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Queue question

---------------------------------------------

A two parter newbie question I am afraid.

Am I right in thinking that using something like ...

item = a_queue.get()
print item

will not print 'item' unless or until there is an item in the queue to
retrieve. Effectively stalling the thread at the .get() instruction?

I can see that this it may be tested for, using the .empty() function
thereby allowing the code to progress if there was nothing in the
queue, but how is .not_empty used ?

Thanks in advance
---------------------------------------------

Oct 15 '05 #1
4 1780
According to my "Python in a Nutshell":

q.get(block=True)

is the signature, so, as you use it above, the call will hang until
something is on the queue. If block is false and the queue is empty,
q.get() will raise the exception Empty.

q.get_nowait is apparently synonymous with q.get(block=False)
q.not_empty, if it existed, I expect would be true just in case there
was at least one item in the queue. But according to my book there is
q.empty and q.full, which is true when the queue has the maximum
allowed number of items (a value specified when the queue is created).

Also, I don't think you can rely on q.empty in the way you may expect.
For example, another thread can empty the queue between the time you
test whether q.empty is false and the time you call q.get.

Oct 15 '05 #2
Ahh the penny has dropped at last. I am using the WingIDE and
..not_empty is one of the properties it exposes with it's intellisense.
As such its use is not documented. No problem.

Using the exception would more accurate - I can see that. In my simple
case the queue is a one to one link, into and out of a single thread
and so I can use the .empty() call with impunity. I can see that using
queue's may be overkill in this instance but it has been good practice
to get to know how they operate.

Being new to python I am just getting used to syntax and conventions,
your reply has clarrified the issue for me.

Thank you for posting Steve.

Oct 16 '05 #3
Steve M <sj******@gmail.com> wrote:
According to my "Python in a Nutshell":

q.get(block=True)

is the signature, so, as you use it above, the call will hang until
something is on the queue. If block is false and the queue is empty,
q.get() will raise the exception Empty.

q.get_nowait is apparently synonymous with q.get(block=False)
Yep. Nowadays you can also have an optional timeout= argument to the
..get method, to obtain the Empty exception only after the get attempt
has waited for some time for some item to arrive.

q.not_empty, if it existed, I expect would be true just in case there
was at least one item in the queue. But according to my book there is
q.empty and q.full, which is true when the queue has the maximum
allowed number of items (a value specified when the queue is created).
not_empty and not_full are not methods but rather instances of the
threading.Condition class, which gets waited on and notified
appropriately. I'm not entirely sure exactly WHAT one is supposed to do
with the Condition instances in question (I'm sure there is some design
intent there, because their names indicate they're public); presumably,
like for the Lock instance named 'mutex', they can be used in subclasses
that do particularly fiendish things... but I keep planning not to cover
them in the 2nd edition of the Nutshell (though there I _will_ cover the
idea of subclassing Queue to implement queueing disciplines other than
FIFO without needing to worry about synchronization, which I had skipped
in the 1st edition).

Also, I don't think you can rely on q.empty in the way you may expect.
For example, another thread can empty the queue between the time you
test whether q.empty is false and the time you call q.get.


Absolutely true. "Anything can happen" right after you call q.empty(),
so the existence of that method isn't a good idea (it's somewhat of an
"attractive nuisance" -- its existence prompts some programmers who
don't read docs to try and use it, possibly producing unreliable code
which works when tested but silently breaks in real-life use).
Alex
Oct 17 '05 #4
[Alex Martelli]
...
not_empty and not_full are not methods but rather instances of the
threading.Condition class, which gets waited on and notified
appropriately. I'm not entirely sure exactly WHAT one is supposed to do
with the Condition instances in question (I'm sure there is some design
intent there, because their names indicate they're public); presumably,
like for the Lock instance named 'mutex', they can be used in subclasses
that do particularly fiendish things... but I keep planning not to cover
them in the 2nd edition of the Nutshell (though there I _will_ cover the
idea of subclassing Queue to implement queueing disciplines other than
FIFO without needing to worry about synchronization, which I had skipped
in the 1st edition).


Last time it was rewritten, I put as much thought into the names of
Queue instance variables as Guido put into them originally: none.
They have always "looked like" public names, but I'm at a loss to
think of anythng sane a Queue client or extender could do with them.
Of course that's why the docs don't mention them. I suppose an
extender could make good use of `mutex` if they wanted to add an
entirely new method, say:

def get_and_put_nowait(self, new_item) :
"""Pop existing item, and push new item, atomically [blah blah blah]."""

I don't know of anyone who has done so, though. I kept the name
`mutex` intact during the last rewrite, but didn't hesitate to get rid
of the former `esema` and `fsema` attributes. Since no complaints
resulted, it's a pretty safe bet nobody was mucking with esema or
fsema before.
Oct 17 '05 #5

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

Similar topics

16
by: newsock | last post by:
What differences between queue, deque and priority_queue? And under what situations choose one of them to use? Thanks for your help!
4
by: ~Gee | last post by:
Hi, When I try to compile the following program, I get the following error: $ g++ anotherTest.C anotherTest.C: In function `int main()': anotherTest.C:47: void value not ignored as it ought to...
7
by: unified | last post by:
Ok, I'm working on a program that is supposed to compare each letter of a string that is put into a stack and a queue. It is supposed to tell whether or not a word is a palindrome or not. (a...
3
by: kelkel | last post by:
Hi all, I got a question here which about using queue. i have create a class of queue. Public Sub Add(ByVal data As Object) SyncLock Me _Data.Enqueue(data) Monitor.Pulse(Me) End SyncLock End...
3
by: palm | last post by:
Hi I have a system.collection.queue object. One thread X enque objects into this queue and another thread Y dequeues object and process them. I want to add a listener for this queue in thread Y...
0
by: Dave Coate | last post by:
I am working on a generic way to launch multiple similar processes (threads) at once, but limit the number of threads running at any one time to a number I set. As I understand it the following...
19
by: ern | last post by:
I need a FIFO queue of size 20, to keep track of a running average. Once the queue is full with 20 values, I want to take the sum/20 = AVERAGE. Then when a new value comes in, I want: (Old Sum...
7
by: Michael D. Ober | last post by:
When calling Enqueue, the internal array may need to be reallocated. My question is by how much? In the old MFC array classes, you could tell MFC how many additional elements to add to the array...
11
by: Terry | last post by:
Hello! I'm trying to implement a message queue among threads using Queue. The message queue has two operations: PutMsg(id, msg) # this is simple, just combine the id and msg as one and put it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.