ipc pickle

Hugo Martires hugomartires at hotmail.com
Mon Feb 25 19:41:53 CET 2002


This only receive twice.
If i need to receive more than twice that is a problem.
See this:

class Client2:
     def __init__(self, file):
         self.file = file
     def read(self):
         # assume size repr is less than 20 bytes
         x = os.read(self.file, 20)
         pos = x.find('.')
->=8000  size_expected = int(x[:pos])
         if size_expected > 20:
             # read rest of data
->sockets    more_data = client.socket.read(self.file, size_expected -
pos )
             return cPickle.loads(x[pos+1:] + more_data)
         else:
             return cPickle.loads(x[pos+1:])
         

I want to send a big vector it's size could be more or less than 8000.
The client to receive the entire vector must receive several times
since BUFFER SIZE it's not so big. (do you know it's maximum value ?)
So the server send :
# Send: size of vector; separator (.) ; vector in a string
CliSock.send( "%d.%s" %(vp_size, vp.tostring() ) )       

My client code:
------------------------

HOST='10.10.22.245'
PORT=21567
ADDR=(HOST , PORT)
BUFSIZE=1024
c=socket(AF_INET,SOCK_STREAM)
c.connect(ADDR)
data_vector=array.array('d')

data = c.recv(BUFSIZE)
sep = data.find('.')    # find the separator in string received
size = int ( data[:sep] )       # get the vector size
while (1):
        if size > BUFSIZE:
            #read rest of data
            data=data + c.recv(BUFSIZE)
        else:
            print data
data = data[sep+1:]
print data
data_vector.fromstring( data )

That's not working

Tanks for help


gerson.kurz at t-online.de (Gerson Kurz) wrote in message news:<3c7a278a.13899093 at news.isar.de>...
> Here is a suggestion:
> ------------------------------------------------
> import cPickle
> import StringIO
> import struct
> import os
> 
> # your classes
> class Client:
>     def __init__(self, file):
>         self.file = file
>     def read(self):
>         # read length and load object
>         x = os.read(self.file, 4)
>         length = struct.unpack('i', x)[0]
>         buf = os.read(self.file, length)
>         f = StringIO.StringIO(buf)
>         return cPickle.load(f)
>         
> class Server:
>     def __init__(self, file):
>         self.file = file
> 
>     def write(self, obj):
>         # write length of serialozed object, write serialized object
>         f = StringIO.StringIO()
>         cPickle.dump(obj, f, 1)
>         val = f.getvalue()
>         length = len(val)
>         plen = struct.pack("i", length)
>         os.write(self.file, plen)
>         os.write(self.file,val)
> 
> # my classes        
> class Client2:
>     def __init__(self, file):
>         self.file = file
>     def read(self):
>         # assume size repr is less than 20 bytes
>         x = os.read(self.file, 20)
>         pos = x.find('.')
>         size_expected = int(x[:pos])
>         if size_expected > 20:
>             # read rest of data
>             more_data = os.read(self.file, size_expected - pos )
>             return cPickle.loads(x[pos+1:] + more_data)
>         else:
>             return cPickle.loads(x[pos+1:])
>         
> class Server2:
>     def __init__(self, file):
>         self.file = file
> 
>     def write(self, obj):
>         test = cPickle.dumps(obj)
>         # interestingly, this seems to be slower:
>         #os.write(self.file,"%d." % len(test) + test)
>         os.write(self.file,"%d.%s" % (len(test),test))
> 
> # test script
> if __name__=='__main__':
>     rp, wp = os.pipe()
> 
>     import time
> 
>     test_string = u"this is a test string that is more than 20 bytes
> long"
> 
>     c = Client(rp)
>     s = Server(wp)
>     start = time.clock()
>     for i in xrange(3000):
>         s.write(test_string)
>         assert(test_string==c.read())
>     print time.clock() - start
> 
>     c = Client2(rp)
>     s = Server2(wp)
>     start = time.clock()
>     for i in xrange(3000):
>         s.write(test_string)
>         assert(test_string==c.read())
>     print time.clock() - start
> 
> 
> # results:
> 0.518367439587 (your)
> 0.228796436055 (mine)
> -------------------------------------------------------
> 
> But, your version seems reasonably fast already :). My "Full-Blown
> RPCServer" that uses dedicated threads for function calls (so that one
> RPC call can issue another RPC call while not blocking other threads
> making their RPC calls etc) is *a lot* slower...



More information about the Python-list mailing list