If you look up __del__ in the Python reference, you'll see that Python gives no 
assurance that destructors will be called for objects which exist when the 
Python interpreter quits.  POX follows this model and does not try to "fix" it. 
 I can think of two solutions off the top of my head:

1) Use atexit.  See the Python documentation for more information.

2) Listen to core!GoingDownEvent or core!DownEvent.  POX fires the first one 
when it's trying to shut down and the second one when it's pretty much down.

My recommendation would be to use core!DownEvent when possible.  In your case, 
I think you can do something along the lines of changing your __del__(self) 
into _handle_GoingDownEvent(self, event) and putting a core.addListeners(self) 
into your __init__.

-- Murphy

On May 12, 2013, at 3:46 PM, Karthik Sharma wrote:

> I have a class called MyController where I use the destructor to print some 
> statistics.
> 
> class MyController (object):
>   def __init__ (self, connection):
>     self.connection = connection
>     connection.addListeners(self)
>     # Use this table to keep track of which ethernet address is on
>     # which switch port (keys are MACs, values are ports).
>     self.mac_to_port = {}
>     # This will keep track of the traffic matrix. 
>     # matrix[i][j]=number of times a packet from i went to j
>     self.matrix={}
>     self.total_ins_time = 0
>     self.total_query_time = 0
>     self.total_ins_count = 0
>     self.total_query_count = 0
>   def __del__(self):
>         print "avg insert time ",(self.total_ins_time/self.total_ins_count);
>         print "avg query time",(self.total_query_time/self.total_query_count);
> 
> 
> I have a function 
> 
> def act_like_switch (self, packet, packet_in):
> 
> which implements the logic.It is called from 
> 
>  def _handle_PacketIn (self, event):
> 
> Is the destructor of the MyController class a good place to print the 
> statistics that I collect during the running of the controller? If not where 
> should I do it? and also why is the destructor of MyController class not 
> being called.As you would have guessed,I am new to Python.
> 
> Regards,
> Karthik.

Reply via email to