473,467 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Overwriting / reassigning instance / garbage collection?

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?????



Jul 18 '05 #1
9 2025
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
Jul 18 '05 #2
Hi,

Ken Godee wrote:

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?????


of course, the old one will eventually be garbage collected.

The question is, what do you actually want to do?
hth

Werner

Jul 18 '05 #3
>>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?


Jul 18 '05 #4
Werner Schiendl wrote:
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?????


of course, the old one will eventually be garbage collected.

The question is, what do you actually want to do?


Actually, all I want to do is "clear" the queue.

I saw some other queue modules and they include
a method to clear the queue.
ie...

q1.clear()

but as far as I can tell the standard included Queue module
that comes with python lacks this method.

Jul 18 '05 #5
Ken Godee wrote:
Werner Schiendl wrote:
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?????

of course, the old one will eventually be garbage collected.

The question is, what do you actually want to do?


Actually, all I want to do is "clear" the queue.

I saw some other queue modules and they include
a method to clear the queue.
ie...

q1.clear()

but as far as I can tell the standard included Queue module
that comes with python lacks this method.


A queue is a data structure with the restriction that elements are added
to it on one end and removed on the other (also known as first-in
first-out, because the first element added will be the first to be removed).

The Queue in the Queue module is not just a queue data structure - it is
designed to be safely shared between multiple program threads, with only
only one thread allowed to access it at one time. The idea is to ensure
that requests from 1 or more thread are handled in order, and none of
them are lost. Therefore, it usually doesn't make sense to clear the
queue (deliberately losing all elements)

Those other queue modules you saw were probably implementing only the
data structure, and not the multi-threaded aspects of Queue.Queue.

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


Jul 18 '05 #6
>
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
Jul 18 '05 #7
Ken Godee wrote:
try:
while 1:
q1.get(0)
except Queue.Empty:
pass [snip]

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.


Ah, there's your problem. You're trying to optimize before you have
a working solution *or* actual measurements showing a performance
problem.

My strong suggestion is write a little clearQueue() routine as
above, and call that, and only worry about the whole thing if you
ever notice a serious performance issue. Most likely you won't.

-Peter
Jul 18 '05 #8
Hi,

Ken Godee wrote:
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.


The only thing you need to pay attention too is that both ends use the
same variable to access the queue.

If you were to pass the queue as a parameter, your sender would continue
to write to the old one after you replaced it.

This is probably a case where a global variable is appropriate.
hth

Werner

Jul 18 '05 #9
>
try:
while 1:
q1.get(0)
except Queue.Empty:
pass [snip] 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.

Ah, there's your problem. You're trying to optimize before you have
a working solution *or* actual measurements showing a performance
problem.


You're right I don't have the actual measurements yet, but
when it goes live the queue will be receiving possibly 100's+
of events per minute, while my program may just sit there
waiting to send a command anywhere from minutes to hours.
Meanwhile the queue if not limited in size or zero'ed out just prior
to sending a command could grow to thousands+ of items.
Once the command is sent, I'm only interested in filtering out
the responses from the command sent. Multiple responses from the command
sent return almost instantly, mixed in with other responses from the
system.
Unfortunately the back end of this system doesn't allow one to only
receive responses from the user who sent it, but receives all responses
from all events on the system.

I experimented last night, with just limiting the size of the queue
and then just before issuing my command reassigning the instance of
the queue. ie....

q1 = Queue.Queue(200)

blah..
program running, q1 filled
q1.qsize()
200
(top of loop)
wait for command input
....getting ready to run my command
q1 = Queue.Queue(200)
q1.qsize()
0
run command
process q1
loop back

Whichs brings me back to original post....
I was worried about the inner workings of
how python would garbage collect the previous instance
of q1, since the first q1 vs 2nd q1 had dif. reference
addresses.
After monitoring the memory usage with the above
code it would appear python handles it quite well
and reuses the memory it already allocated.
Now, I've never had to do this type of
thing with a class instance before, so I
was trying to find out any problems.

Jul 18 '05 #10

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

Similar topics

4
by: MS | last post by:
Hi, In my Genetic Algorithm program, I have a class called Genome. Another class called GA has a class variable called 'population' which is an array of type Genome. One of the methods in GA,...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
6
by: Jason Bell | last post by:
How can I find out how many references to an instance exist at any given time? I want to make it so that when the number of references is one, a countdown starts (that one reference being the...
6
by: woger151 | last post by:
If I write $x = new some_class($init_data1); and then $x = new some_class($init_data2); does the first object (constructed with $init_data1) get destroyed, or do I have to call unset() for...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.