So tonight I had to implement variable sharing between a Perl and Python script. Easy, right? Just use memcached.
Well, it was easy to setup… but not so easy to implement. I installed Python and Perl memcached modules the regular old ways ( you can google this yourself). Sharing worked perfectly between two Python programs and between two Perl programs, but not between Python and Perl.
So I telnetted in to the Memcached server and did some research.
telnet localhost 11211
The results were interesting. When the variable was set with Python:
VALUE test 2 2
Whereas Perl it was set as:
VALUE test 0 2
Notice the difference in the third word – Python has a 2, Perl has a 0. This is a flag thats set, and in order to have communication you need to be able to read the flag.
How to fix this? Easy, hack the Python library to override the flags. Find your memcache library location by doing import memcache; print memcache; That will print out the location. Mine was /usr/lib/python2.7/dist-packages
Now edit this line in memcache.py ( you may need to sudo): https://github.com/samuel/python-memcached/blob/master/memcache.py#L138
Change that line to _FLAG_INTEGER = 0<<1
That overrides the flag to 0. You can go ahead and override any of the other flags too, just replace the leftmost one by 0 ( or even the entire bit shift command , e.g. replace 2<<1 with just 0 ).
Enjoy your coding!