ipc pickle

Janez Jere janez.jere at void.si
Mon Feb 25 06:28:02 EST 2002


Hi, I am experimenting with IPC, my current implementation is very
slow, so I am asking for some hints, how to optimize it. 
Currently I know that cStringIO would cut execution time for 30%. but
I don't want to drop unicode support

Thanks in advance, Janez

import cPickle
import StringIO
#unicode required
#import cStringIO as StringIO
import struct
import os

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)

if __name__=='__main__':
    rp, wp = os.pipe()
    c = Client(rp)
    s = Server(wp)
    s.write(u'\u0161\u010durek')
    print repr(c.read())

    import time
    start = time.clock()
    for i in xrange(3000):
        s.write('a')
        c.read()
    print time.clock() - start





More information about the Python-list mailing list