报错 too many connections
from redis_db import pool
import redis
import random
from concurrent.futures import ThreadPoolExecutor
s = set()
while True:
if len(s) == 1000:
break
n = random.randint(10000, 100000)
s.add(n)
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()
pipline.rpush("kill_user", user_id)
pipline.execute()
except Exception as e:
print(e)
finally:
if 'pipline' in dir():
pipline.reset()
del con
for i in range(0, 1000):
executor.submit(buy)
print("秒杀已经结束!")
正在回答
这个也错了呢
- 参与学习 人
- 提交作业 625 份
- 解答问题 2669 个
数据库作为企业储存和管理数据的根本,掌握数据库是每个开发工程师必备的技能,本阶段带你学会用Python操作MySQL、Redis和MongoDB三大主流数据库。夯实数据库基础。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星