- threading initials
This commit is contained in:
parent
f06e974777
commit
9aeafdfc9e
@ -7,6 +7,8 @@ from watchdog.events import FileSystemEventHandler
|
||||
import os
|
||||
import requests
|
||||
import json
|
||||
import threading
|
||||
import queue
|
||||
|
||||
class SyncClient:
|
||||
@classmethod
|
||||
@ -19,12 +21,12 @@ class SyncClient:
|
||||
def start(self) -> None:
|
||||
_sself = self
|
||||
|
||||
class FileChangeHandler(FileSystemEventHandler):
|
||||
EVENTS_WATCHED = [ "moved", "deleted", "created", "modified" ]
|
||||
self.__event_queue = queue.Queue()
|
||||
|
||||
def comm_thread_routine():
|
||||
while True:
|
||||
event = self.__event_queue.get()
|
||||
|
||||
def on_any_event(self, event):
|
||||
if event.event_type in FileChangeHandler.EVENTS_WATCHED:
|
||||
if event.src_path != _sself.dir():
|
||||
rel_path = str(event.src_path).removeprefix(_sself.dir())
|
||||
if event.event_type == "created" or event.event_type == "modified":
|
||||
_sself.upload(rel_path)
|
||||
@ -37,6 +39,17 @@ class SyncClient:
|
||||
_sself.delete(rel_path)
|
||||
pass
|
||||
|
||||
|
||||
class FileChangeHandler(FileSystemEventHandler):
|
||||
EVENTS_WATCHED = [ "moved", "deleted", "created", "modified" ]
|
||||
|
||||
def on_any_event(self, event):
|
||||
if event.event_type in FileChangeHandler.EVENTS_WATCHED:
|
||||
if event.src_path != _sself.dir():
|
||||
_sself.__event_queue.put(event)
|
||||
|
||||
self.__comm_thread = threading.Thread()
|
||||
|
||||
observer = Observer()
|
||||
event_handler = FileChangeHandler()
|
||||
observer.schedule(event_handler, self.__dir, recursive=True)
|
||||
@ -51,6 +64,9 @@ class SyncClient:
|
||||
self.__id = id
|
||||
self.__key = key
|
||||
|
||||
self.__comm_thread: threading.Thread
|
||||
self.__event_queue: queue.Queue
|
||||
|
||||
self.start()
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user