执行代码后提示too many connections 和watched variable changed
#coding:utf-8
from concurrent.futures import ThreadPoolExecutor
from redis_db import pool
import redis
import random
s=set()
while True:
if len(s)>=1000:
break
num=random.randint(10000,100000)
s.add(num)
con=redis.Redis(
connection_pool=pool
)
try:
con.delete("kill_total","kill_num","kill_flag","kill_user")
con.set("kill_total",50)
con.set("kill_num",0)
con.set("kill_flag",1)
con.expire("kill_flag",600)
except Exception as e:
print(e)
finally:
del con
executor = ThreadPoolExecutor(200)
def buy():
connection = redis.Redis(connection_pool=pool)
pipline = connection.pipeline()
try:
if connection.exists("kill_flag") == 1:
pipline.watch("kill_num", "kill_user")
total = int(pipline.get("kill_total").decode("utf-8"))
num = int(pipline.get("kill_num").decode("utf-8"))
if num<total:
pipline.multi()
pipline.incr("kill_num")
user_id=s.pop()
print(user_id)
pipline.rpush("kill_user",user_id)
pipline.execute()
except Exception as e:
print(e)
finally:
if "pipline" in dir():
pipline.reset()
del connection
for i in range(1000):
executor.submit(buy)
print("秒杀已经结束")
54
收起
正在回答
1回答
同学,你好!
1、出现too many connections是因为redis_db中的pool在建立时有一个参数max_connections,这个参数是最大连接数。若参数的值设置的过小,就会提示too many connections,需要改大一些,例如200,如下图
2、Watched variable changed:watch 执行之后, execute执行之前,有其他客户端修改了watch中指定的key的值, 程序的事务就会抛出异常 redis.WatchError
祝学习愉快!
Python全能工程师
- 参与学习 人
- 提交作业 16246 份
- 解答问题 4470 个
全新版本覆盖5大热门就业方向:Web全栈、爬虫、数据分析、软件测试、人工智能,零基础进击Python全能型工程师,从大厂挑人到我挑大厂,诱人薪资在前方!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星