Hire top tech talent with our recruitment platform

Access Free Demo
piller_image

Getting Started with Python and Redis

This is  a small tutorial to start using Redis with Python. We will look at the steps that tell you how to install Redis in your local ubuntu machine. As we are compiling from source this should also represent how to install in any linux distribution. Then, we can look at some basic commands in Redis and get a feel of the Redis commands. Finally, we will install the Redis-py module and see how we can interface with the Redis module from Python.

Before going through this tutorial, please go through our Redis infographic to get an overview of the Redis module and how to approach it. We also have a webinar on developing a Redis module.

I am using an ubuntu machine. Let us start with the installation of Redis.

sudo apt-get update

Install build essentials which have C and C++ compilers and other GNU C libraries which will help us in the installation later.

sudo apt-get install build-essential

Tcl is also needed to run Redis.

sudo apt-get install tcl8.5

Download the Redis source and untar it.

wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable

We will need to now compile from source.

make 
make test
make install

Once this is done, there will be a set of scripts that will be created in the utils folder which can be used to install the service.

?  utils sudo ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server.


Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
 Adding system startup for /etc/init.d/redis_6379 ...
   /etc/rc0.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc1.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc6.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc2.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc3.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc4.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc5.d/S20redis_6379 -> ../init.d/redis_6379
Success!
Starting Redis server...
Installation successful!

To access Redis, we will need the redis-cli.

?  utils redis-cli
127.0.0.1:6379> exit

We can set and get key values using the “SET” and “GET” keywords.

127.0.0.1:6379> SET users:GeorgeWashington "lang: python, born:1990"
OK
127.0.0.1:6379> GET users:GeorgeWashington
"lang: python, born:1990"
127.0.0.1:6379> exit

Let’s now install the Python module and try to see if we can access the Redis server from Python.

Let’s test the Redis server with the Python.

?  redis_tutorial virtualenv venv -p python3.5
Running virtualenv with interpreter /usr/bin/python3.5
Using base prefix '/usr'
New python executable in venv/bin/python3.5
Also creating executable in venv/bin/python
Installing setuptools, pip...done.
?  redis_tutorial source venv/bin/activate
(venv)?  redis_tutorial
(venv)?  redis_tutorial
(venv)?  redis_tutorial pip install redis
Downloading/unpacking redis
  Downloading redis-2.10.5-py2.py3-none-any.whl (60kB): 60kB downloaded
Installing collected packages: redis
Successfully installed redis
Cleaning up...
(venv)?  redis_tutorial python
Python 3.5.2 (default, Jul 17 2016, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.StrictRedis()
>>> r.get("mykey")
>>> r.get("mykey")
>>> r.get("users:GeorgeWashington")
b'lang: python, born:1990'

This was a basic tutorial about using Python with Redis. Next, we will lay a frontend for making a twitter client using Flask as the middleware and Redis in the backend.

References:

agiliq, getting started with redis and python
digitalocean, how to install and use redis

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

Related reads

Top 8 Sourcing Tools for Recruiters: A Comprehensive Guide
Top 8 Sourcing Tools for Recruiters: A Comprehensive Guide

Top 8 Sourcing Tools for Recruiters: A Comprehensive Guide

In today’s competitive talent landscape, attracting top candidates requires going beyond traditional job board postings. This is where effective sourcing tools comes into…

Benefits of Technical Interview Outsourcing for Growing Companies
Benefits of Technical Interview Outsourcing for Growing Companies

Benefits of Technical Interview Outsourcing for Growing Companies

With growth, recruiting the best technical talents becomes one of the most important, but also the hardest, processes. Screening technical candidates requires time,…

Enterprise Recruitment – Process & Challenges
Enterprise Recruitment – Process & Challenges

Enterprise Recruitment – Process & Challenges

In recent years, recruitment practices have changed tremendously. As the times advanced, organisations took numerous steps towards adopting technology-based recruitment, addressing the various…

Leveraging Recruitment Metrics to Improve Hiring Decisions
Leveraging Recruitment Metrics to Improve Hiring Decisions

Leveraging Recruitment Metrics to Improve Hiring Decisions

Today’s job market is very competitive. Organizations must adopt data-driven approaches to amplify their recruitment efforts to stay afloat in the face of…

The Impact of Talent Assessments on Reducing Employee Turnover
The Impact of Talent Assessments on Reducing Employee Turnover

The Impact of Talent Assessments on Reducing Employee Turnover

Organizations of all industries struggle with employee turnover. The high turnover rates cause increased hiring costs, lost productivity, and broken team dynamics. That’s…

Pre-Employment Assessment Testing – The Complete Guide
Pre-Employment Assessment Testing – The Complete Guide

Pre-Employment Assessment Testing – The Complete Guide

Candidate assessment is a major part of the hiring process. The talent acquisition system emphasizes conducting pre-employment assessment testing to derive quality results….

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

View More

Top Products

Hackathons

Engage global developers through innovation

Hackerearth Hackathons Learn more

Assessments

AI-driven advanced coding assessments

Hackerearth Assessments Learn more

FaceCode

Real-time code editor for effective coding interviews

Hackerearth FaceCode Learn more

L & D

Tailored learning paths for continuous assessments

Hackerearth Learning and Development Learn more