定时任务程序调用其他类中的方法报错:Working outside of application context
来源:5-5 flask上下文与with语句
慕少0293275
2020-12-16
定时任务调用的函数
def test():
print("开始时间"+ ": " +(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
obj = rabbitmqAPI()
obj.check_rabbit_diff
print("结束时间"+ ": " +(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
函数中调用的方法,变量有使用 current_app
def __init__(self):
self.localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self.url = current_app.config['URL']
self.auth = (current_app.config['USERNAME'], current_app.config['PASSWORD'])
self.datas = self.get_manager_datas()
def check_rabbit_diff(self):
rabbit_name = self.add_wrong_data_to_sql()
sql_name = self.get_allname_from_sql()
diff_name = list(set(sql_name) ^ set(rabbit_name))
if len(diff_name) != 0:
for i in diff_name:
Rabbitmq.update_data(i, self.localtime)
return "Finish"
定时任务运行时,代码报错了:Working outside of application context
我要如何处理才能解决这个报错?
写回答
1回答
-
7七月
2020-12-16
这是在哪运行的代码?应该手动把程推入到上下文中。结合下课程开始讲的上下文原理
042020-12-16
相似问题