You've already forked WebSocketTry
mirror of
https://github.com/Llloooggg/WebSocketTry.git
synced 2026-03-06 12:36:23 +03:00
30 lines
667 B
Python
30 lines
667 B
Python
import json
|
|
|
|
import pyinotify
|
|
|
|
from .. import socketio
|
|
|
|
thread = None
|
|
|
|
|
|
class ModHandler(pyinotify.ProcessEvent):
|
|
def process_IN_CLOSE_WRITE(self, evt):
|
|
with open("data/data.json") as json_file:
|
|
data = json.load(json_file)
|
|
socketio.emit("file_updated", data)
|
|
|
|
|
|
def background_thread():
|
|
handler = ModHandler()
|
|
wm = pyinotify.WatchManager()
|
|
notifier = pyinotify.Notifier(wm, handler)
|
|
wm.add_watch("data/data.json", pyinotify.IN_CLOSE_WRITE)
|
|
notifier.loop()
|
|
|
|
|
|
@socketio.on("connect")
|
|
def update_data():
|
|
global thread
|
|
if thread is None:
|
|
thread = socketio.start_background_task(target=background_thread)
|