Overwriting / reassigning instance / garbage collection?

Ken Godee ken at perfect-image.com
Thu Oct 30 16:23:29 EST 2003


> 
> If you understand this, and *really* do want to discard all the elements 
> from a Queue.Queue, you can just do:
> 
> try:
>     while 1:
>         q1.get(0)
> except Queue.Empty:
>     pass
> 
> However, you should be aware that any other parts of the program which 
> have references to q1 can always add more elements to q1, so it won't 
> necessarily stay empty.  Also, if you do have a multi-threaded 
> application, and other threads are adding elements to the Queue faster 
> than the code above can remove them, the code above might never finish.
> 
> David
> 
yes, I understand this.
and that's why I'm wondering if de-referencing the instance
as below would work and that the old address space would
get garabage collected. This would then happen in a fraction
of the time that it might take the while statement to empty
the queue, if ever. I'm not building a heart monitor, just trying
to find a way to buffer the messages and then use them on a FIFO basis.
The Queue module is perfect for what I'm trying to do and even
takes care of the locking issues.
Well, heck, now that I'm thinking about it again, I'm not sure
how well the function that's puttting the messages into the queue
is going to like it if I try the recreate instance method, argggg.....
Might just have to use the while method anyway. Maybe pause sending
messages in some form while the queue empties.

q1 = Queue.Queue()
q1.put('test1')
q1.put('test2')
q1.qsize()
2
q1 = Queue.Queue()
q1.qsize()
0






More information about the Python-list mailing list