[Tutor] Finding an object by ID
alan.gauld at btinternet.com
Thu May 3 00:51:34 CEST 2007
"Dj Gilcrease" <digitalxero at gmail.com> wrote
>I was wondering if it was possible to find and object by it's ID.
Its probably possible but almost certainly the wrong thing to do...
> what I want to do is something like
> The reason I want to do this is right now I store all the data in a
> dict of class
> class connections:
> def __init__(self, *args, **kwargs):
> self.sock = args
> self.myotherdata = stuff
> def incomingConnection(self, stuff):
> (incSock, incAdder) = self.__sock.accept()
> self.conns[incAdder] = connections(incSock, ...)
> This takes up about 20 megs per person connecting, which is very
But I'm not sure hpow storing IDs will help, the objects still
need to be in memory. The only saving is in the actual
dictionary itself which is relatively small.
Also you could probably save memory more easily by
storing the data to disk using pickle or shelve, but you'd need
to be very sure that you could retrieve the objects fast enough
to process your incoming connections - what is the peak rate?
> IMHO, and I am hoping by storing all the data in the database and
You only seemed to be storing the ID - which is basically the
memory location. If you are going to store all the data in memory
then the ID as a key is pointless you might as well get the database
to generate one for you or use a simple counter at clkass level
because you will need to regenerate the instance in memory to
use it and that will result in it having a different ID...
If you really need to reduce footprint you should probably
consider connection object pooling where you use a cache
of connection objects and reuse them. That way you only need
the number of connections that are active at any one time - and
you will always need those regardless of what you do with
the inactive ones. Also look at the format of your data, is that
in the most efficient format? Could it be encoded using struct
for example? Or zipped?
Author of the Learn to Program web site
More information about the Tutor