473,544 Members | 1,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why the method get() of python Queue is hang on there?

Hi,
I am using Queue from python2.4. Here is what happen to me:

import Queue
b = Queue.Queue(0)
b.put(9999)
b.get() # this is ok, it pops out 9999
b.get() # this one does not return anything and is hang on there

Anybody knows what is going on with the second b.get()?

ouyang

Aug 14 '06 #1
6 11409
zxoimport Queue
zxob = Queue.Queue(0)
zxob.put(9999)
zxob.get() # this is ok, it pops out 9999
zxob.get() # this one does not return anything and is hang on there

zxoAnybody knows what is going on with the second b.get()?

Queue objects are meant to be used in a multithreaded application. By
default, when the Queue is empty, a consumer calling get() will block until
a producer put()s something else into it. From the documentation:

get([block[, timeout]])
Remove and return an item from the queue. If optional args block is
true and timeout is None (the default), block if necessary until an
item is available....

Skip
Aug 14 '06 #2
import Queue
b = Queue.Queue(0)
b.put(9999)
b.get() # this is ok, it pops out 9999
b.get() # this one does not return anything and is hang on
there

Anybody knows what is going on with the second b.get()?
>>help(Queue.Qu eue)
:
:
| get(self, block=True, timeout=None)
| Remove and return an item from the queue.
|
| If optional args 'block' is true and
| 'timeout' is None (the default), block if
| necessary until an item is available. If
| 'timeout' is a positive number, it blocks
| at most 'timeout' seconds and raises the
| Empty exception if no item was available
| within that time. Otherwise ('block' is
| false), return an item if one is
| immediately available, else raise the
| Empty exception ('timeout' is ignored in
| that case).
|
| get_nowait(self )
| Remove and return an item from the queue
| without blocking.
|
| Only get an item if one is immediately
| available. Otherwise raise the Empty
| exception.
:
:

Notice that by default, get() will block until
there's something to read. You can use either

b.get(block=Fal se)

or

b.get_nowait()

In either case, an exception will be raised when
the Queue is empty, to let you know as much. Just
trap for the exception and do as you please.

-tkc


Aug 14 '06 #3
zxo102 wrote:
Hi,
I am using Queue from python2.4. Here is what happen to me:

import Queue
b = Queue.Queue(0)
b.put(9999)
b.get() # this is ok, it pops out 9999
b.get() # this one does not return anything and is hang on there

Anybody knows what is going on with the second b.get()?
the documentation has the answer:

get( [block[, timeout]])

Remove and return an item from the queue. If optional args block
is true and timeout is None (the default), block if necessary until
an item is available. /.../

http://docs.python.org/lib/QueueObjects.html

</F>

Aug 14 '06 #4
On Mon, 14 Aug 2006 17:10:13 +0200, zxo102 <zx****@gmail.c omwrote:
Hi,
I am using Queue from python2.4. Here is what happen to me:

import Queue
b = Queue.Queue(0)
b.put(9999)
b.get() # this is ok, it pops out 9999
b.get() # this one does not return anything and is hang on there

Anybody knows what is going on with the second b.get()?
What did you expect? Since you've done only one put in the Queue, there's
nothing left in it. Since queues are meant to communicate between threads
- and also often to synchronize them -, the default behaviour for the get
when the queue is empty is to wait for something to be put in it. If you
want your second get to fail, use the get_nowait method.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"
Aug 14 '06 #5
Thanks for your guys. I got it. I thought Queue can be used anywhere
in the code and the second b.get() would return a "None".

Ouyang
zxo102 写道:
Hi,
I am using Queue from python2.4. Here is what happen to me:

import Queue
b = Queue.Queue(0)
b.put(9999)
b.get() # this is ok, it pops out 9999
b.get() # this one does not return anything and is hang on there

Anybody knows what is going on with the second b.get()?

ouyang
Aug 14 '06 #6
At Monday 14/8/2006 12:35, zxo102 wrote:
>Thanks for your guys. I got it. I thought Queue can be used anywhere
in the code and the second b.get() would return a "None".
You can use a list as a generic queue, with append (== push) and pop(0)

Gabriel Genellina
Softlab SRL

_______________ _______________ _______________ _____
Pregunt. Respond. Descubr.
Todo lo que queras saber, y lo que ni imaginabas,
est en Yahoo! Respuestas (Beta).
Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 14 '06 #7

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

Similar topics

0
2250
by: TT (Tom Tempelaere) | last post by:
Hi, In my project I have a seperate logging application. I use remoting to log to it. The remoted object exposes a certain (log-)interface, and the methods therein are all OneWay methods (decorated with the OneWay attribute). In my real application, I connect to the remoted log-object (using RemotingServices.Connect). This returns a proxy...
4
6396
by: mike | last post by:
Printer - HP4300 in 3 different locations. VB.Net Calling crReportObject.PrintToPrinter(iRptCopies, True, 1, -1) doesn't always return. We have had at least 4 cases now where the printers above seem to hang with several reports in the printer queue and when this happens the method PrintToPrinter doesn't return, thus hanging the...
4
1361
by: mike | last post by:
Printer - HP4300 in 3 different locations. VB.Net Calling crReportObject.PrintToPrinter(iRptCopies, True, 1, -1) doesn't always return. We have had at least 4 cases now where the printers above seem to hang with several reports in the printer queue and when this happens the method PrintToPrinter doesn't return, thus hanging the...
36
1841
by: danielx | last post by:
At first I was going to post the following: <!-- beginning of my original post --> I just discovered the inspect module, which contains the isfunction and ismethod functions. For some reason, I used to be under the impression that Python methods are no different from Python functions. Naturally, I wondered why both of these needed to...
0
7426
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, well explore What is ONU, What Is Router, ONU & Routers main...
0
7368
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...
1
7381
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...
0
5914
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5299
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3418
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...
0
3412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
989
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
667
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...

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.