I would like to develop a python application on Windows that will use Redis as a broker for Celery. Is it correct to assume that my application can interact with an instance of Redis that I have installed on the Windows Subsystem for Linux?
I have enabled the Windows Subsystem for Linux on Windows 10, and installed Ubuntu and Redis onto it, and started the server. On the Windows side, I’m using VSCode to write the python code. As shown below, in my python code, I am trying to connect to Redis on localhost:6379
from celery import Celery BROKER_URL = 'redis://localhost:6379/0' app = Celery('tasks', broker=BROKER_URL)
I am trying to confirm whether my Python code, written in Windows, can interact with the Redis server being run on Ubuntu. Is this possible, and if so how can I confirm the connection?
Advertisement
Answer
Yes, you can use redis from wsl from windows. First, make sure you’ve installed and started the redis service:
sudo apt-get install redis-server sudo systemctl enable redis-server.service
if you already run a redis server on windows, you’ll need to edit the port directive in /etc/redis/redis.conf (e.g. to 7379 like I’ve done for the commands below).
Then start the service
sudo service redis-server start
and then run redis-cli
and issue the monitor
command (you can skip the -p 7379
if you’re using the default port):
bp@bjorn8:~$ redis-cli -p 7379 127.0.0.1:6379> monitor OK
now, from your windows command prompt, install the redis module from pypi (https://pypi.org/project/redis/):
pip install redis
then start python and issue a test command (again, use 6379 if you’re using the default port):
>>> import redis >>> cn = redis.Redis('localhost', 7379, 0) >>> cn.keys("*") []
in your wsl session you should now see something like:
1558196107.718695 "KEYS" "*"
Note: redis is not fuzzy about where the server is. If you have the cli tools installed on windows you can issue commands from dos to the server running on wsl:
c:srv> redis-cli -p 7379 redis 127.0.0.1:7379> keys "*" (empty list or set) redis 127.0.0.1:7379>
and vice-versa (redis-cli on wsl will happily connect to a redis service running on windows — which is how I discovered I needed to specify different ports ;-)