engine.py 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : app.py
# @Time       : 2020/11/18 下午12:45
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------


import logging
import redis
import os
import sys
import yaml
from sqlalchemy import create_engine
17
from sqlalchemy.orm import sessionmaker, scoped_session
18 19 20 21 22 23 24 25 26 27 28 29

env = sys.argv[-1]
work_dir = os.getcwd()
CFG_FILEPATH = work_dir + '/app/config/config.yaml'

config = yaml.load(open(CFG_FILEPATH, 'r'), Loader=yaml.FullLoader)

tamp_user_engine = create_engine(
    'mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
        db=config[env]['MySQL']['tamp_user_db'],
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
30
        user=config[env]['MySQL']['user'],
31 32
        password=config[env]['MySQL']['password'],
        charset="utf8"
33 34
    ),
    max_overflow=0,  # 超过连接池大小外最多创建的连接
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
35 36
    pool_size=100,  # 连接池大小
    pool_timeout=5,
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
37
    pool_recycle=600
38
)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
if env == 'prod':
    tamp_user_engine = create_engine(
        'mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
            db=config[env]['MySQL']['tamp_user_db'],
            host=config[env]['MySQL']['host'],
            port=config[env]['MySQL']['port'],
            user=config[env]['MySQL']['user2'],
            password=config[env]['MySQL']['password'],
            charset="utf8"
        ),
        max_overflow=0,  # 超过连接池大小外最多创建的连接
        pool_size=100,  # 连接池大小
        pool_timeout=5,
        pool_recycle=600
    )

55 56 57 58 59 60 61
tamp_pay_engine = create_engine(
    'mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
        db=config[env]['MySQL']['tamp_pay_db'],
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
62 63
        charset="utf8"),
    max_overflow=0,  # 超过连接池大小外最多创建的连接
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
64 65
    pool_size=100,  # 连接池大小
    pool_timeout=5,
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
66
    pool_recycle=600
67 68
)

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
69
redis = redis.Redis(
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
70 71
    host=config[env]['redis']['host'],
    port=config[env]['redis']['port'],
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
72 73
    db=config[env]['redis']['db'],
    password=config[env]['redis']['password']
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
74
)
75 76

logging.basicConfig(level=logging.INFO,
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
77 78
                    # filename=work_dir + config[env]['log']['filename'],
                    # filemode=config[env]['log']['filemode'],
79 80 81 82
                    format=config[env]['log']['format'],
                    datefmt=config[env]['log']['datefmt']
                    )

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

class TAMP_SQL(object):
    """[sqlalchemy 封装]

    Args:
        object ([type]): [description]

    Returns:
        [type]: [description]
    """

    def __init__(self, db_engine):
        # 创建DBSession类型:
        self.DBSession = scoped_session(sessionmaker(bind=db_engine))
        self.session = self.DBSession()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        try:
            self.session.commit()
        except:
            self.session.rollback()
        finally:
            self.session.close()