This bit of code hangs Python Indefinitely

B

brad

url_queue = Queue.Queue(256)
for subnet in subnets:
url_queue.put(subnet)

The problem is that I have 512 things to add to the queue, but my limit
is half that... whoops. Shouldn't the interpreter tell me that I'm an
idiot for trying to do this instead of just hanging? A message such as
this would be more appropriate:

"Hey fool, you told me to only accept 256 things and you're trying to
give me 512... what's up with that?"
 
M

Marc 'BlackJack' Rintsch

url_queue = Queue.Queue(256)
for subnet in subnets:
url_queue.put(subnet)

The problem is that I have 512 things to add to the queue, but my limit
is half that... whoops. Shouldn't the interpreter tell me that I'm an
idiot for trying to do this instead of just hanging? A message such as
this would be more appropriate:

"Hey fool, you told me to only accept 256 things and you're trying to
give me 512... what's up with that?"

No because you can get some things from that queue from another thread and
then the ``for``-loop goes on. That's the typical use case for queues.

Why did you put an upper bound to the queue?

Ciao,
Marc 'BlackJack' Rintsch
 
I

Istvan Albert

The problem is that I have 512 things to add to the queue, but my limit
is half that... whoops. Shouldn't the interpreter tell me that I'm an
idiot for trying to do this instead of just hanging? A message such as
this would be more appropriate:


See the docs, especially the block and timeout parameter for the put
method:

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


Istvan
 
C

Chris Mellon

url_queue = Queue.Queue(256)
for subnet in subnets:
url_queue.put(subnet)

The problem is that I have 512 things to add to the queue, but my limit
is half that... whoops. Shouldn't the interpreter tell me that I'm an
idiot for trying to do this instead of just hanging? A message such as
this would be more appropriate:

"Hey fool, you told me to only accept 256 things and you're trying to
give me 512... what's up with that?"

You should have read the Queue documentation first. Queues, by design,
are producer/consumer streams and the producer is designed to block if
the queue is full. You can use the put_nowait method to have it raise
an exception instead of blocking.
 
B

brad

Chris said:
... the producer is designed to block if
the queue is full. You can use the put_nowait method to have it raise
an exception instead of blocking.

I assumed that the behavior would have been the other way around. I
should not have made that assumption.
 
B

brad

Marc said:
Why did you put an upper bound to the queue?

For clarity. Explicit is better than implicit, right? In our design, the
queue should only have x number of things, so why not show that? Other
than that, the limit is arbitrary and is not needed.
 
D

Duncan Booth

brad said:
I assumed that the behavior would have been the other way around. I
should not have made that assumption.

You could possibly make a case that before Queue.put blocks it should check
whether the program has more than just the one thread and if not it should
raise an RTFMException.
 
S

Steve Holden

brad said:
For clarity. Explicit is better than implicit, right? In our design, the
queue should only have x number of things, so why not show that? Other
than that, the limit is arbitrary and is not needed.

Yes, but you are asking a bit more of Queue.Queue than its design is
intended to provide. Normally a limit on the input side is used to allow
the scheduling of a consumer thread that will remove items, thus
allowing the producer to restart.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
P

Paddy

You could possibly make a case that before Queue.put blocks it should check
whether the program has more than just the one thread and if not it should
raise an RTFMException.

Cute :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top