python 多线程 redis查询

使用多线程查询redis数据

from concurrent.futures import ThreadPoolExecutor
import redis

# 查询函数在每个线程中创建新的Redis连接
def query_redis(key):
    client = redis.StrictRedis(host='localhost', port=6379, db=0)
    return client.get(key)

# 创建一个键列表
keys = ['key1', 'key2', 'key3', 'key4']

# 使用线程池执行查询
with ThreadPoolExecutor(max_workers=4) as executor:
    results = list(executor.map(query_redis, keys))

print(results)

你可能感兴趣的:(python,redis)