[py-svn] r32813 - py/dist/py/execnet

arigo at codespeak.net arigo at codespeak.net
Mon Oct 2 12:52:50 CEST 2006


Author: arigo
Date: Mon Oct  2 12:52:49 2006
New Revision: 32813

Modified:
   py/dist/py/execnet/message.py
Log:
A simple execnet optimization to avoid using repr/eval in the case of a
direct channel.send("string").  This seems to be a common pattern when
exchanging large quantities of data, so it's probably a good idea.



Modified: py/dist/py/execnet/message.py
==============================================================================
--- py/dist/py/execnet/message.py	(original)
+++ py/dist/py/execnet/message.py	Mon Oct  2 12:52:49 2006
@@ -5,8 +5,9 @@
 #
 # Messages
 # ___________________________________________________________________________
-# the size of a number on the wire
-numsize = struct.calcsize("!i")
+# the header format
+HDR_FORMAT = "!hhii"
+HDR_SIZE   = struct.calcsize(HDR_FORMAT)
 
 class Message:
     """ encapsulates Messages and their wire protocol. """
@@ -16,26 +17,31 @@
         self.data = data
 
     def writeto(self, io):
-        # big XXX and all this
-        #     data = marshal.dumps(self.data)
-        # doesn't work for exchanging data across Python version :-(((
-        data = repr(self.data)  # argh
-
-        header = struct.pack("!iii", self.msgtype, self.channelid, len(data))
-        io.write(header)
-        io.write(data)
+        # XXX marshal.dumps doesn't work for exchanging data across Python
+        # version :-(((  There is no sane solution, short of a custom
+        # pure Python marshaller
+        data = self.data
+        if isinstance(data, str):
+            dataformat = 1
+        else:
+            data = repr(self.data)  # argh
+            dataformat = 2
+        header = struct.pack(HDR_FORMAT, self.msgtype, dataformat,
+                                         self.channelid, len(data))
+        io.write(header + data)
 
     def readfrom(cls, io):
-        header = io.read(numsize*3)
-        msgtype, senderid, stringlen = struct.unpack("!iii", header)
-        if stringlen:
-            string = io.read(stringlen)
-            # same big XXX as in writeto()
-            #     string = marshal.loads(string)
-            string = eval(string, {})
+        header = io.read(HDR_SIZE)
+        (msgtype, dataformat,
+         senderid, stringlen) = struct.unpack(HDR_FORMAT, header)
+        data = io.read(stringlen)
+        if dataformat == 1:
+            pass
+        elif dataformat == 2:
+            data = eval(data, {})   # reversed argh
         else:
-            string = ''
-        msg = cls._types[msgtype](senderid, string)
+            raise ValueError("bad data format")
+        msg = cls._types[msgtype](senderid, data)
         return msg
     readfrom = classmethod(readfrom)
 



More information about the pytest-commit mailing list