Selaa lähdekoodia

feat: 刷新缓存只在第一次requests前启动

SongZihuan 2 vuotta sitten
vanhempi
sitoutus
d401c572c9
1 muutettua tiedostoa jossa 11 lisäystä ja 8 poistoa
  1. 11 8
      main.py

+ 11 - 8
main.py

@@ -24,14 +24,17 @@ from sql.cache_refresh import refresh
 restart_clear_cache()  # 清理缓存
 
 
-class FirstRefresh(threading.Thread):
-    def run(self):
-        refresh()
-
-first_refresh_th = FirstRefresh()
-first_refresh_th.start()
-refresh_th = threading.Timer(conf["CACHE_REFRESH_INTERVAL"], refresh)
-refresh_th.start()
+@app.before_first_request
+def before_first_requests():
+    class FirstRefresh(threading.Thread):
+        def run(self):
+            refresh()
+
+    first_refresh_th = FirstRefresh()
+    first_refresh_th.start()
+    refresh_th = threading.Timer(conf["CACHE_REFRESH_INTERVAL"], refresh)
+    refresh_th.start()
+
 
 if __name__ == '__main__':
     logging.info("Server start on 127.0.0.1:8080")