You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python连接Redis成功但执行set/get操作时卡顿无响应

问题:Python连接本地Redis后无法执行set/get操作

背景

目标:通过Python成功对本地Redis执行set()和get()键值对操作。
已确认信息:

  • Redis连接可正常建立,无论是否携带redis_db、redis_max_connections参数
  • 6379端口处于开放状态
  • 终端可正常执行SET、GET等Redis命令:
127.0.0.1:6379> SET my-key TESTVAL
OK
127.0.0.1:6379> GET my-key
"TESTVAL"
127.0.0.1:6379> DEL my-key
(integer) 1
127.0.0.1:6379> GET my-key
(nil)

因此判定问题出在Python代码层面。

Python代码

import redis

redis_host = 'localhost'  # 127.0.0.1
redis_port = 6379
redis_password = ''  # 'your-redis-password'
redis_db = 2
redis_max_connections = 100

client = redis.Redis(host=redis_host, port=redis_port, password=redis_password, ssl=True, db=redis_db,
                           max_connections=redis_max_connections)
print('Connected!')

key = 'KEY'
value = 'VALUE'

client.set(key, value)
print('Data stored on Redis with key: ', key)

data = client.get(key)
print('Data retrieved from Redis with key: ', key)
print(data)

代码运行结果

(venv) me@laptop:~/GitHub/project$ python3 foo/bar/minimal_working_example.py
Connected!
|

程序输出Connected!后卡住,无后续内容输出。

Redis服务器状态确认

Redis服务正常运行:

(base) me@laptop:~$ redis-server
4965:C 17 Jan 2023 09:36:56.119 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4965:C 17 Jan 2023 09:36:56.119 # Redis version=7.0.7, bits=64, commit=00000000, modified=0, pid=4965, just started
4965:C 17 Jan 2023 09:36:56.119 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
4965:M 17 Jan 2023 09:36:56.119 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4965:M 17 Jan 2023 09:36:56.119 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4965
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4965:M 17 Jan 2023 09:36:56.120 # Server initialized
4965:M 17 Jan 2023 09:36:56.120 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4965:M 17 Jan 2023 09:36:56.120 * Ready to accept connections

终端连通性测试

(base) me@laptop:~$ redis-cli
127.0.0.1:6379> ping
PONG
(base) me@laptop:~$ nc -z localhost 6379
(base) me@laptop:~$ telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

解决方案

问题根源是代码中设置了ssl=True——本地Redis默认未启用SSL加密,开启SSL后Python客户端会尝试建立加密连接,导致程序卡住无响应。

修改代码,将ssl=True改为ssl=False(或直接删除该参数,默认值即为False):

client = redis.Redis(host=redis_host, port=redis_port, password=redis_password, ssl=False, db=redis_db,
                           max_connections=redis_max_connections)

修改后重新运行代码,即可正常执行set和get操作,预期输出如下:

(venv) me@laptop:~/GitHub/project$ python3 foo/bar/minimal_working_example.py
Connected!
Data stored on Redis with key:  KEY
Data retrieved from Redis with key:  KEY
b'VALUE'

内容的提问来源于stack exchange,提问作者DanielBell99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:40:46