PicoRedis - a very minimal Redis client library (not only) for MicroPython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

PicoRedis - a very minimal Redis client library (not only) for MicroPython

Post by SpotlightKid » Tue Nov 28, 2017 8:56 pm

As a little programming exercise, I wrote a very small Redis client library for MicroPython:

https://github.com/SpotlightKid/picoredis

I tested it on the esp8266 and unix port and on CPython 2/3 and PyPy 2/3. Python 2 support is accidental and not guaranteed, though. :)

On my ESP8266 board running the latest Git version of MicroPython, importing the pre-compiled `picoredis.mpy` module, uses up about 3800 bytes of memory. A Redis instance will use a further ~750 bytes when created and dynamically allocate more memory when parsing responses from the Redis server (which can get quite big), but this memory should be collected by the GC again after the response has been returned.

Code: Select all

MicroPython v1.9.3-55-g4ffe3e7b on 2017-11-26; ESP module with ESP8266
Type "help()" for more information.
>>> 
>>> import gc; gc.collect()
>>> gc.mem_free()
29120
>>> from picoredis import Redis
>>> gc.mem_free()
25296
>>> 29120 - 25296
3824
>>> redis = Redis('192.168.1.100')
>>> gc.mem_free()
24544
>>> redis.ping('hello world')
b'hello world'
>>> gc.mem_free()
24816
The full code of the module has only about 100 SLOC. It doesn't aim to be a fully compatible Redis client, but the basic Redis commands which work on keys, hash maps, sets and so on can be used, because the Redis protocol uses only string and integer values everywhere. When using PicoRedis the developer must take care of converting the byte string values returned by the Redis server to what the application needs. See the link above for some usage examples.

Share & Enjoy!

Post Reply