N
N
nekolyanich2010-11-01 12:23:46
PHP
nekolyanich, 2010-11-01 12:23:46

Problem communicating php and python via memcached?

if a set is made from php to memcache, then when I try to do a get by such a key from python, I get:

&gt;&gt;&gt; import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))<br/>
/usr/bin/python2.6 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) <br/>
[GCC 4.4.5]<br/>
&gt;&gt;&gt; import memcache<br/>
&gt;&gt;&gt; import binascii<br/>
&gt;&gt;&gt; m=memcache.Client(['127.0.0.1:11211'], debug=0)<br/>
&gt;&gt;&gt; def php_hash(key):<br/>
... return (binascii.crc32(key) &gt;&gt; 16) & 0x7fff<br/>
... <br/>
&gt;&gt;&gt; m.get((php_hash(mfd.php_nexttime), mfd.php_nexttime))<br/>
Traceback (most recent call last):<br/>
 File &quot;&quot;, line 1, in NameError: name 'mfd' is not defined<br/>
&gt;&gt;&gt; m.get((php_hash('mfd.php_nexttime'), 'mfd.php_nexttime'))<br/>
Traceback (most recent call last):<br/>
 File &quot;&quot;, line 1, in  File &quot;/usr/lib/pymodules/python2.6/memcache.py&quot;, line 779, in get<br/>
 return self._get('get', key)<br/>
 File &quot;/usr/lib/pymodules/python2.6/memcache.py&quot;, line 766, in _get<br/>
 value = self._recv_value(server, flags, rlen)<br/>
 File &quot;/usr/lib/pymodules/python2.6/memcache.py&quot;, line 915, in _recv_value<br/>
 return val<br/>
UnboundLocalError: local variable 'val' referenced before assignment<br/>
<br/>
Устал гуглить может кто ни будь сталкивался?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2010-11-01
@nekolyanich

Apparently you will have to get into the source code of the python client and fix everything related to flags there. In flags (768 and 300 for example), clients store information about how they encoded (serialized) the data. see github.com/memcached/memcached/blob/master/doc/protocol.txt#L155
In your case VALUE mfd.php_nexttime 768 10the flag is 768 and 10 is the response length (10 bytes). I just didn’t quite understand why you have different flags for the same key (because there was the same value both times ??)
If you use the same serialization methods on both ends (php-python), look at what flags the PHP client marks them with and replace them in the Python client with similar ones or use the default text flag. More specifically, I’m unlikely to tell you. there you just need to poke around in the code and edit it. (see the _recv_value(self, server, flags, rlen) function in the client sources, you can write val = buf as an option for

else:
            val = buf# like this!!
            self.debuglog("unknown flags on get: %x\n" % flags)
)

S
standov, 2010-11-01
@standov

python probably tries to deserialize the value right away and breaks off - the syntax of the php serializer is not familiar to him ... the decision to push the encoded value into the json memcache and decode it in python, well, or any other "standard format"

S
Sergey, 2010-11-01
@seriyPS

I took a walk through the library code... Try first to make memcache.debug=True and see what it writes to stderr.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question