Overwriting / reassigning instance / garbage collection?

Ken Godee ken at perfect-image.com
Thu Oct 30 13:51:36 EST 2003


>>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?
> 
I know, I'm reaching.... I'll try a short explaination...

The code I'm working on has a registered call back in it
that is communicating with a module that is threaded and continuosly
sending event messages back to my function.

def myfunc(event):
     print event.headers

manager.Event.register("*", myfunc)

Which of coarse for instance if I put a print statement in my call back 
func. you would see the constaint flow of event messages on standard out.
Now later in my code I only want to try to catch a few of these event 
messages that have to do with the commands I'm sending, so in an effort
to buffer(kinda), "Queue"

q1 = Queue.Queue()

def myfunc(event):
     q1.put(event.headers)

manager.Event.register("*", myfunc)

paused, waiting to send command
commands sent.......

while q1.qsize > 0:
     x = q1.get()
     if x['Event'] == 'Call':
         etc...
     if x['Event'] == 'Hangup':
         break etc....
....
At this point the program is going to loop
and be waiting to send more commands.

I can filter out what I want, but since
this program remains active (still filling the queue)
I see the queue filling with alot of unneeded info and to as how big, 
who knows.

So, what I was thinking was to set a default size for
the queue q1 = Queue.Queue(200) and when ready to
use, clear it.
Since there's no "clear" method, and the queue is only being
referenced by the single instance, I was toying with
just recreating, I don't know, but it seems like the first
reference is then wiped out and since the object would then
have no reference count, I would think python would gc the first one?








More information about the Python-list mailing list