[Python] can not connect to redis-server installed on wsl2 of windows 11

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I use windows 11 and install redis server on wsl2 of windows 11 and try to connect to redis using pyspark and redis python api. Belows are the python codes

    from pyspark.sql import SparkSession
    import redis

    r = redis.Redis(host='localhost', port=6379) # connected to redis server successfully
    print(r.info) # it prints <redis.client.Redis(<redis.connection.ConnectionPool(<redis.connection.Connection(host=localhost,port=6379,db=0)>)>)>>

    spark = SparkSession.builder.appName("SparkRedisExample").getOrCreate()

    data = [("user:1001","Alice",30),("user:1002","Bob",25),("user:1003","Charlie",35)]
    df = spark.createDataFrame(data, ["user_id","name","age"])

    def process_partition(rows):
    # Executor-local redis client
    client = redis.Redis(host='localhost', port=6379) # This line throws errors. can not connected to redis server
    for row in rows:
    client.hset(row.user_id, mapping={"name": row.name, "age": row.age})

    df.foreachPartition(process_partition)


    the 1st line of codes r = redis.Redis(host='localhost', port=6379) is connected to redis successfully. But the connection of client = redis.Redis(host='localhost', port=6379) in process_partition function can not be connected to redis server. I can not understand why these errors occur. Kindly inform me how to connect to redis-server on wsl2 with these codes.

    Continue reading...

Compartilhe esta Página