Django通过Json配置文件分配多个定时任务

python 复制代码
	def load_config():
	    with open("rule.json", 'rb')as f:
	        config = json.load(f)
	    return config
	def job(task_name, config, time_interval):
	# 	...	通过task_name判断进行操作
		if task_name == 'get_data_times':
			pass

	def main():
	    config = load_config()
	
	    for task_name, task_value in config.items():
	        # 循环配置文件中配置的时间 启动定时任务			多少秒运行一次
	        if task_name.startswith('get_data') or task_name.startswith('get_internet'):
	            time_interval = int(task_value.replace('s', ''))
	            schedule.every(time_interval).seconds.do(job, task_name, config, time_interval)
	        elif task_name.endswith('_time'):
	        	# 多少分钟运行一次
	            time_interval = int(task_value.replace('min', ''))
	            time_intervals = time_interval + 1
	            schedule.every(time_interval).minutes.do(job, task_name, config, time_intervals)
			# 可以设置多个 时分秒 周月星期一到星期天 at
	    def run_jobs():
	        while True:
	            schedule.run_pending()
	            time.sleep(1)
	
	    thread = threading.Thread(target=run_jobs)
	    thread.start()
	
	if __name__ == '__main__':
	    import django, os
	    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "QsManagement.settings")
	    django.setup()
	    from app01.logger import logger
	    from app01.models import ComputerStatus, AlarmInfo, InterNetSpeed, WindowsError, ProductionLine, Machine, MachineType
	    from app01.methods import get_ip
	    main()

json配置文件 通过配置文件设置阈值,到达指定阈值报警

python 复制代码
	{
	  "get_data_times": "3min",
	  "get_internet_times": "3min",
	  "get_windows_error_time": "3min",
	
	  "cpu_time" : "1min",
	  "cpu_rate" : "90%",
	
	  "memory_time" : "1min",
	  "memory_rate" : "75%",
	
	  "gpu_time" : "1min",
	  "gpu_rate" : "50%",
	
	  "disk_times": "3min",
	  "C_disk_space": "50G",
	  "D_disk_space": "30G",
	  "E_disk_space": "50G",
	  "F_disk_space": "50G",
	  "G_disk_space": "50G",
	
	  "cpu_temperature":  "50度",
	  "temperature_time": "1min",
	
	  "internet_speed": "200ms",
	  "internet_time" : "1min"
	}
相关推荐
春山之外14 分钟前
基于IIS的Windows系统Django项目本地部署
后端·python·django·iis·服务器部署
zDarkBlue36 分钟前
VSCode工程中task.json的作用
ide·vscode·json
zhangjin11205 小时前
HttpURLConnection发送POST请求传递JSON参数
json
杰哥在此6 小时前
Python面试题:如何在 Python 中读取和写入 JSON 文件?
开发语言·python·面试·json·编程
2301_786964367 小时前
Django文档简化版——Django快速入门——创建一个基本的投票应用程序(3)
数据库·python·django·sqlite·html
木觞清11 小时前
Django学习第三天
python·学习·django
BBJG_00118 小时前
cpp struct json相互转换
c++·测试开发·json·struct·class
南通DXZ20 小时前
对MsgPack与JSON进行序列化的效率比较
json
吹吹晚风-1 天前
深入Django(六)
数据库·django·sqlite
He BianGu1 天前
笔记:Newtonsoft.Json 自定义序列化反序列化规则
笔记·json