import json import multiprocessing import time import redis import config import sell_price import fangjia import datetime import pytz import os QUEUE = "calc_cmd" redis_pool = redis.ConnectionPool(host=config.get_redis_host(), port=6379, db=config.get_redis_db()) logger = fangjia.get_logger("CalcCmdReceiver") def worker(): client = redis.Redis(connection_pool=redis_pool) while True: try: cmd = client.lpop(QUEUE) except Exception as ex: cmd = None print(ex) if cmd is None: time.sleep(60) logger.info("等待接受计算命令") else: cmd = format_cmd(cmd) start_time = time.clock() try: sell_price.calc_sell_price(cmd) end_time = time.clock() logger.info("Calc SUC:" + str(cmd) + "\t计算成功,耗时:" + str(end_time - start_time)) except Exception as ex: print(ex) logger.exception(ex) end_time = time.clock() logger.error("Calc ERROR :" + str(cmd) + "\t计算失败,耗时:" + (end_time - start_time)) def format_cmd(cmd): cmd = json.loads(cmd) cmd["weekend"] = datetime.datetime.strptime(cmd["weekend"], "%Y-%m-%d") pacific = pytz.timezone('Asia/Shanghai') cmd["weekend"] = pacific.localize(cmd["weekend"]) return cmd if __name__ == "__main__": user_home = os.path.expanduser('~') lock_file = user_home + "/price.sys.stop" if os.path.exists(lock_file): os.remove(lock_file) pro_num = config.get_worker_num() #pro_num = 1 pool = multiprocessing.Pool(processes=pro_num) for pid in range(0, pro_num): pool.apply_async(worker, ()) pool.close() logger.info("均价系统启动成功") while True: if os.path.exists(lock_file): pool.terminate() break else: time.sleep(3) if os.path.exists(lock_file): os.remove(lock_file) pool.join()