pickle problem

krustymonkey at gmail.com krustymonkey at gmail.com
Sun May 11 19:21:47 EDT 2008


On May 8, 7:29 pm, castiro... at gmail.com wrote:
> On May 8, 4:35 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
>
>
>
> > Marc 'BlackJack' Rintsch <bj_... at gmx.net> writes:
>
> > > On Thu, 08 May 2008 08:55:35 -0700, krustymonkey wrote:
>
> > >> The thing is, I'm not using slots by choice.  I'm using the standard
> > >> lib "socket" class, which apparently uses slots.
>
> > > `socket` objects can't be pickled.  Not just because of the
> > > `__slot__`\s but because a substantial part of their state lives in
> > > the operating system's space.
>
> > Of course, if it makes sense to pickle sockets in the application, one
> > is can do so by defining __getstate__ and __setstate__:
>
> > class Connection(object):
> >     def __init__(self, host, port):
> >         self.host = host
> >         self.port = port
> >         self.init_sock()
>
> >     def init_sock(self):
> >         self.sock = socket.socket()
> >         self.sock.connect((host, port))
> >         ... init communication ...
>
> >     def __getstate__(self):
> >         # pickle self as a (host, port) pair
> >         return self.host, self.port
>
> >     def __setstate__(self, state):
> >         # reinstate self by setting host and port and
> >         # recreating the socket
> >         self.host, self.port = state
> >         self.init_sock()
>
> I, local, am mystified that you'd want to pickle a socket.  It's a
> live connection, which flies on a pocket dollar.  You don't want it on
> disk, do you?
>
> If you're running a net buoy through a cluster somewhere, do you want
> to drop a server and reconnect?  Is Amazon's EC2 up and running?
>
> Certainly no one was talking on the internet.  Were you?
>
> I don't think you hit anything but banks surfing the web, and the
> American dollar is notoriously stuffy.  Pump a wi-fi to a diner,
> though, and you're just talking more.  Where's Usenet?

The idea is a pre-fork socket server.  What I want to do is fork off
some child processes from the parent and use IPC to send the
connection object (via socket.accept()) over a pipe to one of the
preexisting child processes and have said child handle the
transaction.  Think Apache, if you want an example of what I'm trying
to do here.  This alleviates the process startup cost that occurs when
you fork.  If the socket object can't be serialized, is there another
way to go about handling this in python?



More information about the Python-list mailing list