# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: ExecutorJobPool.py
# @Author: Leslie Cheung
# @E-mail: leslieswr0820@gmail.com
# @Site: 
# @Time: 2021/2/3 9:59
# ---
from dateutil.parser import parse
from sbh.gshj.util.account import *
from sbh.gshj.util.Dict_date import *
from sbh.gshj.util.LogUitl import *
import datetime as dt
from concurrent.futures import ThreadPoolExecutor,as_completed
log = Logger('auto.log', logging.ERROR, logging.DEBUG)
def Run_Sql_Month(list_time,list_func_name,routine_schema='dwd'):
    '''
    :param list_time: 时间列表 格式：'202001'
    :param list_function: 函数列表:"fun_dw_inp_drgs_patient_m"
    :return: None
    '''
    try:
        for f_name in list_func_name:
            for t_time in list_time:
                print(list_time.index(t_time))
                with PgSQLContextManager() as db_cursor:
                    start_time = dt.datetime.now()
                    sql = ''' select {routine_schema}."{f_name}"('{day_id}','{day_id}'); '''.format(day_id=t_time,f_name = f_name,routine_schema=routine_schema)
                    log.info("执行sql日期为：{}".format(t_time))
                    log.info(sql)
                    db_cursor.execute(sql)
                    end_date = dt.datetime.now()
                    log.info(f'执行完成时间为：{(end_date-start_time).seconds}s')
    except Exception as e:
        log.info(e)
        log.info("循环结束")


list_time = Get_Time_section_30(list_time=[], cycle_day=60)
# list_function_name =['fun_dwd_D00014_d',
# 'fun_dwd_D00015_d',
# 'fun_dwd_D00016_d',
# 'fun_dwd_D00017_d',
# 'fun_dwd_D00018_d',
# 'fun_dwd_D00019_d',
# 'fun_dwd_D00020_d',
# 'fun_dwd_D00021_d',
# 'fun_dwd_D00022_d',
# 'fun_dwd_D00023_d',
# 'fun_dwd_D00024_d',
# 'fun_dwd_D00025_d',
# 'fun_dwd_D00026_d',
# 'fun_dwd_D00027_d',
# 'fun_dwd_D00028_d',
# 'fun_dwd_D00029_d',
# 'fun_dwd_D00030_d',
# 'fun_dwd_D00031_d',
# 'fun_dwd_D00032_d',
# 'fun_dwd_D00033_d',
# 'fun_dwd_D00034_d',
# 'fun_dwd_D00035_d',
# 'fun_dwd_D00036_d',
# 'fun_dwd_D00037_d',
# 'fun_dwd_D00038_d',
# 'fun_dwd_D00039_d',
# 'fun_dwd_D00040_d',
# 'fun_dwd_D00041_d',
# 'fun_dwd_D00042_d',
# 'fun_dwd_D00044_d',
# 'fun_dwd_D00045_d',
# 'fun_dwd_D00046_d',
# 'fun_dwd_D00048_d',
# 'fun_dwd_D00049_d',
# 'fun_dwd_D00050_d',
# 'fun_dwd_D00051_d',
# 'fun_dwd_D00052_d',
# 'fun_dwd_D00053_d',
# 'fun_dwd_D00088_d',
# 'fun_dwd_D00089_d',
# 'fun_dwd_D00090_d',
# 'fun_dwd_D00091_d',
# 'fun_dwd_D00092_d',
# 'fun_dwd_D00093_d',
# 'fun_dwd_D00094_d',
# 'fun_dwd_D00095_d',
# 'fun_dwd_D00096_d',
# 'fun_dwd_D00100_d',
# 'fun_dwd_D00101_d',
# 'fun_dwd_D00102_d',
# 'fun_dwd_D00103_d',
# 'fun_dwd_D00104_d',
# 'fun_dwd_D00105_d',
# 'fun_dwd_D00106_d',
# 'fun_dwd_D00107_d',
# 'fun_dwd_D00108_d',
# 'fun_dwd_D00109_d',
# 'fun_dwd_D00110_d',
# 'fun_dwd_D00111_d',
# 'fun_dwd_D00112_d',
# 'fun_dwd_D00113_d',
# 'fun_dwd_D00114_d',
# 'fun_dwd_D00115_d',
# 'fun_dwd_D00116_d',
# 'fun_dwd_D00117_d',
# 'fun_dwd_D00118_d',
# 'fun_dwd_D00119_d',
# 'fun_dwd_D00120_d',
# 'fun_dwd_D00121_d',
# 'fun_dwd_D00122_d',
# 'fun_dwd_D00123_d',
# 'fun_dwd_D00124_d',
# 'fun_dwd_D00125_d',
# 'fun_dwd_D00126_d',
# 'fun_dwd_D00127_d',
# 'fun_dwd_D00128_d',
# 'fun_dwd_D00129_d',
# 'fun_dwd_D00130_d',
# 'fun_dwd_D00131_d',
# 'fun_dwd_D00132_d',
# 'fun_dwd_D00133_d',
# 'fun_dwd_D00134_d',
# 'fun_dwd_D00135_d',
# 'fun_dwd_D00136_d',
# 'fun_dwd_D00137_d',
# 'fun_dwd_D00138_d',
# 'fun_dwd_D00139_d',
# 'fun_dwd_D00140_d',
# 'fun_dwd_D00141_d',
# 'fun_dwd_D00142_d',
# 'fun_dwd_D00143_d',
# 'fun_dwd_D00144_d',
# 'fun_dwd_D00145_d',
# 'fun_dwd_D00146_d',
# 'fun_dwd_D00147_d',
# 'fun_dwd_D00148_d',
# 'fun_dwd_D00149_d',
# 'fun_dwd_D00150_d',
# 'fun_dwd_D00151_d',
# 'fun_dwd_D00152_d',
# 'fun_dwd_D00153_d',
# 'fun_dwd_D00154_d',
# 'fun_dwd_D00155_d',
# 'fun_dwd_D00156_d',
# 'fun_dwd_D00157_d',
# 'fun_dwd_D00158_d',
# 'fun_dwd_D00159_d',
# 'fun_dwd_D00160_d',
# 'fun_dwd_D00161_d',
# 'fun_dwd_D00162_d',
# 'fun_dwd_D00163_d',
# 'fun_dwd_D00164_d',
# 'fun_dwd_D00165_d',
# 'fun_dwd_D00166_d',
# 'fun_dwd_D00167_d',
# 'fun_dwd_D00168_d',
# 'fun_dwd_D00169_d',
# 'fun_dwd_D00170_d',
# 'fun_dwd_D00171_d',
# 'fun_dwd_D00172_d',
# 'fun_dwd_D00173_d',
# 'fun_dwd_D00174_d',
# 'fun_dwd_D00175_d',
# 'fun_dwd_D00176_d',
# 'fun_dwd_D00177_d',
# 'fun_dwd_D00178_d',
# 'fun_dwd_D00179_d',
# 'fun_dwd_D00180_d',
# 'fun_dwd_D00181_d',
# 'fun_dwd_D00182_d',
# 'fun_dwd_D00183_d',
# 'fun_dwd_D00184_d',
# 'fun_dwd_D00185_d',
# 'fun_dwd_D00186_d',
# 'fun_dwd_D00187_d',
# 'fun_dwd_D00188_d',
# 'fun_dwd_D00189_d',
# 'fun_dwd_D00190_d',
# 'fun_dwd_D00191_d',
# 'fun_dwd_D00192_d',
# 'fun_dwd_D00193_d',
# 'fun_dwd_D00194_d',
# 'fun_dwd_D00195_d',
# 'fun_dwd_D00196_d',
# 'fun_dwd_D00197_d',
# 'fun_dwd_D00198_d',
# 'fun_dwd_D00199_d',
# 'fun_dwd_D00200_d',
# 'fun_dwd_D00201_d',
# 'fun_dwd_D00202_d',
# 'fun_dwd_D00203_d',
# 'fun_dwd_D00204_d',
# 'fun_dwd_D00205_d',
# 'fun_dwd_D00206_d',
# 'fun_dwd_D00207_d',
# 'fun_dwd_D00208_d',
# 'fun_dwd_D00209_d',
# 'fun_dwd_D00210_d',
# 'fun_dwd_D00215_d',
# 'fun_dwd_D00216_d',
# 'fun_dwd_D00217_d',
# 'fun_dwd_D00218_d',
# 'fun_dwd_D00219_d',
# 'fun_dwd_D00220_d',
# 'fun_dwd_D00221_d',
# 'fun_dwd_D00222_d',
# 'fun_dwd_D00223_d',
# 'fun_dwd_D00224_d',
# 'fun_dwd_D00225_d',
# 'fun_dwd_D00226_d',
# 'fun_dwd_D00227_d',
# 'fun_dwd_D00228_d',
# 'fun_dwd_D00229_d',
# 'fun_dwd_D00230_d',
# 'fun_dwd_D00231_d',
# 'fun_dwd_D00232_d',
# 'fun_dwd_D00233_d',
# 'fun_dwd_D00234_d',
# 'fun_dwd_D00235_d',
# 'fun_dwd_D00236_d',
# 'fun_dwd_D00237_d',
# 'fun_dwd_D00238_d',
# 'fun_dwd_D00239_d',
# 'fun_dwd_D00240_d',
# 'fun_dwd_D00241_d',
# 'fun_dwd_D00242_d',
# 'fun_dwd_D00243_d',
# 'fun_dwd_D00244_d',
# 'fun_dwd_D00245_d',
# 'fun_dwd_D00246_d',
# 'fun_dwd_D00248_d',
# 'fun_dwd_D00250_d',
# 'fun_dwd_D01002_d',
# 'fun_dwd_D01003_d',
# 'fun_dwd_D01004_d',
# 'fun_dwd_D01005_d',
# 'fun_dwd_D01006_d',
# 'fun_dwd_D01007_d',
# 'fun_dwd_D01008_d',
# 'fun_dwd_D01009_d',
# 'fun_dwd_D05000_d',
# 'fun_dwd_D05001_d',
# 'fun_dwd_D05002_d',
# 'fun_dwd_D05003_d',
# 'fun_dwd_D05004_d',
# 'fun_dwd_D05005_d',
# 'fun_dwd_D05006_d',
# 'fun_dwd_D05007_d',
# 'fun_dwd_D05008_d',
# 'fun_dwd_D05009_d',
# 'fun_dwd_D05010_d',
# 'fun_dwd_D05011_d',
# 'fun_dwd_D05012_d',
# 'fun_dwd_D05013_d',
# 'fun_dwd_D05014_d',
# 'fun_dwd_D05015_d',
# 'fun_dwd_D05016_d',
# 'fun_dwd_D05017_d',
# 'fun_dwd_D05018_d',
# 'fun_dwd_D05019_d',
# 'fun_dwd_D05020_d',
# 'fun_dwd_D05021_d',
# 'fun_dwd_D05022_d',
# 'fun_dwd_D05023_d',
# 'fun_dwd_D05024_d',
# 'fun_dwd_D05025_d',
# 'fun_dwd_D05026_d',
# 'fun_dwd_D05027_d',
# 'fun_dwd_D05028_d',
# 'fun_dwd_D05029_d',
# 'fun_dwd_D05030_d',
# 'fun_dwd_D05031_d',
# 'fun_dwd_D05032_d',
# 'fun_dwd_D05033_d',
# 'fun_dwd_D05034_d',
# 'fun_dwd_D05035_d',
# 'fun_dwd_D05036_d',
# 'fun_dwd_D05037_d',
# 'fun_dwd_D05038_d',
# 'fun_dwd_D05039_d',
# 'fun_dwd_D05040_d']
list_function_name = []
n = 1
list_result = [list_function_name[i:i + n] for i in range(0, len(list_function_name), n)]
try:
    with ThreadPoolExecutor(max_workers=15) as t:
        obj_list=[]
        for i in range(len(list_result)):
            obj = t.submit(Run_Sql_Month, list_time, list_result[i], 'his_bi')
            obj_list.append(obj)
        for future in as_completed(obj_list):
            data = future.result()
except:
    t.shutdown()