Overwriting / reassigning instance / garbage collection?

Peter Hansen peter at engcorp.com
Thu Oct 30 12:05:34 EST 2003


Ken Godee wrote:
> 
> I'm using the python Queue module, but since I can not
> find anyway to clear the Queue, I thought I would try
> to reassign ie......
> 
> q1 = Queue.Queue()
> q1.put('test1')
> q1.put('test2')
> q1.qsize()
> 2
> q1 = Queue.Queue()
> q1.qsize()
> 0
> 
> q1 now has a new address, so I'm
> assuming the old q1 instance now has a
> reference count of zero and python will garbage collect it?????

Given that the only decent reason to use a Queue is to communicate
between separate threads, or at least between different parts of
the code, if you rebind the name "q1" to a new instance of Queue,
but you don't do anything with any other names that are bound to
it, it won't work right.  And if you have no other names bound to
it, why do you have a Queue?

(Or are you just referencing the same name from both threads?
In that case, note that you might lose data when one thread is
putting stuff into the old Queue, but the other one is starting
to use the new Queue.  Of course, since your purpose here is to
"clear" the Queue, I suppose that would actually work okay... hmm.)

-Peter




More information about the Python-list mailing list