1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import logging import random from concurrent.futures import ThreadPoolExecutor import redis from models import redis_db
def buy(user_id): try: con_for_buy = redis.Redis( connection_pool=redis_db.pool ) pipline = con_for_buy.pipeline() if con_for_buy.exists("kill_flag"): total_num = int(con_for_buy.get("kill_total").decode("utf-8")) num = int(con_for_buy.get("kill_num").decode("utf-8")) pipline.watch("kill_num", "kill_user") if num < total_num: pipline.incr("kill_num") pipline.lpush("kill_user", user_id) pipline.execute() pipline.reset() except Exception as e: logging.error(e) finally: del con_for_buy
def is_param_exist(): try: con = redis.Redis( connection_pool=redis_db.pool ) if con.exists("kill_total", "kill_flag", "kill_num"): return True else: return False except Exception as e: logging.error(e) return False finally: del con
def create_param(): try: con = redis.Redis( connection_pool=redis_db.pool ) con.set("kill_total", 20) con.set("kill_num", 0) con.set("kill_flag", 1) con.expire("kill_flag",600) return True except Exception as e: logging.error(e) return False finally: del con
def delete_param(): try: con = redis.Redis( connection_pool=redis_db.pool ) con.delete("kill_total","kill_num","kill_flag","kill_user") return True except Exception as e: logging.error(e) return False finally: del con
if __name__ == "__main__": s = set() while True: if len(s) == 1000: break s.add(random.randint(10000,100000))
if delete_param() is not True: exit() if create_param() is not True: exit() if is_param_exist() is not True: exit()
try: executor_ = ThreadPoolExecutor(50) for i in range(0,1000): user_id = s.pop() executor_.submit(buy,user_id) con = redis.Redis( connection_pool=redis_db.pool ) except Exception as e: logging.error(e) finally: del executor_,con
|