开发者社区> 问答> 正文

redis 连接问题

redis经常进行连接就会报错

redis.exceptions.ConnectionError: Error 99 connecting to 127.0.0.1:6379. Cannot assign requested address.
资料上说,redis经常进行连接,但是不关闭,端口不够用了。
所以就去找了,pipeline进行连接,以为这样就能解决频繁的连接问题

def throw_on_redis(data):

r = redis.Redis(config['host'], config['port'], config['db'])
p = r.pipeline()
return p.lpush(config['queue'],data)

def data_by_redis():

r = redis.Redis(config['host'],config['port'],config['db'])
p = r.pipeline()
return p.blpop('urls').execute()

for number in range(100000):

print throw_on_redis(number)

while True:

print data_by_redis()

但是依然不行。
想知道应该怎么处理这种情况,我用redis不可避免的出现频现往里面读(主要是读取)和写这种情况。

展开
收起
李博 bluemind 2019-04-09 11:57:10 2512 0
1 条回答
写回答
取消 提交回答
  • 云栖社区Java、Redis、MongoDB运营小编,有意合作请联系钉钉:15810436147

    //建立了一个连接,但是没有释放。服务器仍然hang住了该tcp连接。

    r = redis.Redis(config['host'], config['port'], config['db'])

    题外话,上面的代码描述来看,其实是不需要每次请求都新建链接的。把redis.Redis(config['host'], config['port'], config['db'])作为全局变量来处理即可。

    2019-07-17 23:32:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Redis在唯品会的应用实践——架构演进与功能定制 立即下载
微博的Redis定制之路 立即下载
云数据库Redis版的开源之路 立即下载