Files
PriceSystem/PriceSystem/CalcCmdReceiver.py
T

68 lines
1.8 KiB
Python

import datetime
import json
import multiprocessing
import os
import time
import pytz
import config
import fangjia
import sell_price
QUEUE = "calc_cmd"
logger = fangjia.get_logger("CalcCmdReceiver")
def worker():
client = fangjia.get_default_redis_connection()
while True:
try:
cmd = client.lpop(QUEUE)
except Exception as ex:
cmd = None
if cmd is None:
time.sleep(60)
logger.info("等待接受计算命令")
else:
cmd = format_cmd(cmd)
start_time = time.time()
try:
sell_price.calc_sell_price(cmd)
end_time = time.time()
logger.info("Calc SUC:" + str(cmd) + "\t计算成功,耗时:" + str(end_time - start_time))
except BaseException as ex:
logger.exception(ex)
end_time = time.time()
logger.error("Calc ERROR :" + str(cmd) + "\t计算失败,耗时:" + str(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()
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()