Multi-threading for performance :D
This commit is contained in:
parent
8ce1cc7885
commit
4bdb160f4d
2 changed files with 41 additions and 14 deletions
30
src/main.py
30
src/main.py
|
|
@ -1,7 +1,9 @@
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from .watcher import Watcher
|
from .watcher import Watcher
|
||||||
|
|
@ -25,14 +27,36 @@ def main(source: str, destination: str) -> None:
|
||||||
logger.info(f"Watching directory {source}")
|
logger.info(f"Watching directory {source}")
|
||||||
logger.info(f"Backing up to {destination}")
|
logger.info(f"Backing up to {destination}")
|
||||||
|
|
||||||
|
threads = [
|
||||||
|
Thread(
|
||||||
|
target=worker,
|
||||||
|
args=(
|
||||||
|
logger,
|
||||||
|
Path(source).absolute().joinpath(dir),
|
||||||
|
Path(destination).absolute().joinpath(dir),
|
||||||
|
),
|
||||||
|
daemon=True,
|
||||||
|
)
|
||||||
|
for dir in filter(
|
||||||
|
lambda x: os.path.isdir(Path(source).absolute().joinpath(x)),
|
||||||
|
os.listdir(source),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
[x.start() for x in threads]
|
||||||
|
[x.join() for x in threads]
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Waiting for threads to die...")
|
||||||
|
pass # keyboard interrupt should be caught by each thread
|
||||||
|
|
||||||
|
|
||||||
|
def worker(logger, source: str, destination: str) -> None:
|
||||||
try:
|
try:
|
||||||
watcher = Watcher(logger, source, destination)
|
watcher = Watcher(logger, source, destination)
|
||||||
logger.info("Watcher started")
|
|
||||||
watcher.start()
|
watcher.start()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("Stopping watcher")
|
|
||||||
watcher.stop()
|
watcher.stop()
|
||||||
logger.info("Watcher stopped -- quitting!")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ from watchdog.events import (
|
||||||
FileSystemMovedEvent,
|
FileSystemMovedEvent,
|
||||||
)
|
)
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
|
from threading import current_thread
|
||||||
|
|
||||||
|
|
||||||
class Watcher:
|
class Watcher:
|
||||||
|
|
@ -29,19 +30,19 @@ class Watcher:
|
||||||
self.handler,
|
self.handler,
|
||||||
self.source,
|
self.source,
|
||||||
recursive=True,
|
recursive=True,
|
||||||
event_filter=[
|
# event_filter=[
|
||||||
FileCreatedEvent,
|
# FileCreatedEvent,
|
||||||
FileDeletedEvent,
|
# FileDeletedEvent,
|
||||||
FileModifiedEvent,
|
# FileModifiedEvent,
|
||||||
FileSystemMovedEvent,
|
# FileSystemMovedEvent,
|
||||||
],
|
# ],
|
||||||
)
|
)
|
||||||
self.logger.info("Custom handler has been scheduled with the Observer")
|
self.observer.setDaemon(True)
|
||||||
# self.observer.setDaemon(True)
|
|
||||||
self.observer.start()
|
self.observer.start()
|
||||||
self.logger.info("The Observer has been started!")
|
self.logger.info(
|
||||||
|
f"[{current_thread().native_id}] The Observer for {self.source} has been started!"
|
||||||
|
)
|
||||||
|
|
||||||
self.logger.info("Starting main loop @ 1s polling rate")
|
|
||||||
self.running = True
|
self.running = True
|
||||||
while self.running:
|
while self.running:
|
||||||
sleep(0.01)
|
sleep(0.01)
|
||||||
|
|
@ -65,7 +66,9 @@ class Handler(FileSystemEventHandler):
|
||||||
event (watchdog.events.FileSystemEvent): the event fired
|
event (watchdog.events.FileSystemEvent): the event fired
|
||||||
"""
|
"""
|
||||||
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
||||||
self.logger.info(f"type: {event.event_type}, src: {event.src_path}{dest_str}")
|
self.logger.info(
|
||||||
|
f"[{current_thread().native_id}] type: {event.event_type}, src: {event.src_path}{dest_str}"
|
||||||
|
)
|
||||||
|
|
||||||
def __in_destination(self, path: Path) -> Path:
|
def __in_destination(self, path: Path) -> Path:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue