让python 动态获取config 内容 无需重启python

admin2天前量化交易536

config.json

{
  "leverage": 1.2
}


python 文件需要获取杠杆的页面

import json


def get_leverage():
    try:
        with open('config.json', 'r') as f:
            config = json.load(f)
            return config.get('leverage', 1)  # 默认值为 1
    except Exception as e:
        print(f"读取配置失败: {e}")
        return 1  # 降级策略


在需要调用的地方

"leverage": get_leverage(),