def bootstrap(): from importlib import import_module args=get_options() os.environ['environment']=args.environment os.environ['main']=str(os.getpid()) # 区分主进程 import config config.config_initialize() from . import router router.run()
import sys import time from datetime import datetime import shlex import subprocess import hashlib import db import config import notary_common if len(sys.argv) < 3: print >> sys.stderr, "show_chain.py <notary.config> <ee_cert_id_from_db>" sys.exit(1) config.config_initialize(sys.argv[1]) db.db_initialize(config.Config) ee_cert_id = int(sys.argv[2]) cursor = db.Db.cursor() sql = "SELECT certificate FROM ee_certs WHERE id=%s LIMIT 1" cursor.execute(sql, (ee_cert_id,)) row = cursor.fetchone() db.Db.commit() if not row: sys.exit("No certificate by that id") ee_cert = str(row['certificate'])
def run(*args, **kwargs): """ """ start_time = time.time() if 'nt' == os.name and os.environ['main'] != str(os.getpid()): config.config_initialize() from utils.RedisHelper import RedisHelper from utils.MysqlHelper import MysqlHelper context = args[0] uid = args[1] r = RedisHelper() p = MysqlHelper() # prod,beta, print(context["data"]["user_id"], 'start uid is ', uid, context) sql = "SELECT `id` FROM `emotion_data` where userid='{}'".format( context["data"]["user_id"]) try: p.cursor.execute(sql) datas = p.cursor.fetchall() except Exception as e: print(repr(e)) datas = [] # 日志报错 # finally: pass # datas=datas[0] # 映射id 待优化 通用型 datas_list = [value[0] for index, value in enumerate( datas) if index % context["data"]["sub_count"] == uid] # 根据uid分配测试数据条例 sql = spell_sql(datas_list) if not sql: return try: p.cursor.execute(sql) datas = p.cursor.fetchall() except Exception as e: print(repr(e)) datas = [] print('datas has ', len(datas)) Result = [[], [], []] for data in datas: request_data = { "title": data[2], "content": data[3], "type": data[7], "cid": data[9], "is_debug": 1, "data": context["data"]["config_data"] } index = datas.index(data) index = datas_list[index] # 请求待优化至通用 request_data = json.dumps(request_data) try: time.sleep(0.01) response = requests.post(url=u_config.NLP_URL, data=request_data, headers=u_config.NLP_HEADERS) if response.status_code != 200: print(request_data) print('continue') sentiment = json.loads(response.text) label = sentiment["emotion"] except Exception as e: print("error continue", repr(e)) # 优化error continue try: p.cursor.execute( f"UPDATE emotion_data SET lastEmotion='{label}' WHERE id={index}") p.conn.commit() except Exception as e: print("UPDATE error", repr(e)) r.conn.hset(context["data"]["task_name"], data[0], json.dumps(sentiment)) Result[0].append(label) # 结果聚合待优化通用 #新机器 Result[1].append(data[1] if "" != data[1] else data[-1]) # 人工 Result[2].append(data[0]) # id r.conn.expire(context["data"]["task_name"],2592000)# 定时一周 r.conn.hset(context["data"]["task_name"], f"info_{uid}", json.dumps( Result )) print(time.time()-start_time)
(options, args) = parser.parse_args() if len(args) < 1: parser.error("Missing configuration file argument") if options.newer is not None and options.older is not None: parser.error("Only one of --newer/--older can be used") if options.newer is not None: threshold_time = datetime.now() - timedelta(days=int(options.newer)) if options.older is not None: threshold_time = datetime.now() - timedelta(days=int(options.older)) config.config_initialize(args[0]) db.db_initialize(config.Config) #Use named (server-side) cursor to avoid too much memory consumed by normal #cursor. Unnamed cursor would load all results into memory even before #fetchone() is called (psycopg behaves this way). cursor = db.Db.cursor(name="list_services") blacklist = set() blacklist_fname = options.blacklist if blacklist_fname: with file(blacklist_fname) as blacklist_file: blacklist = set([line.rstrip() for line in blacklist_file]) try: