python - 使用卷挂载的Docker上的事件监听器不起作用

标签 python docker events python-watchdog

使用以下方式挂载某些文件夹:

docker run -it  C:\Users\User\Documents\project\input:/app/input/ project:latest 

在容器的Python中,一个watchdog.observers实例正在运行,以检测是否将新文件添加到主机文件夹中。这些文件通过卷挂载最终存储在Docker容器中。
文件创建的事件触发器未到达Docker容器。我认为这很奇怪,因为在Docker容器中事件也正在发生吗?

当我在本地运行代码而不是在Docker中运行事件监听器时,它正在运行。

事件监听在我的上下文中有用吗?

main.py
from watch import ImagesWatcher

if __name__ == '__main__':
    src_path = "/app/input/"
    ImagesWatcher(src_path).run()

watch.py
import sys
import time

from watchdog.observers import Observer
from ImagesEventHandler import ImagesEventHandler


class ImagesWatcher:
    def __init__(self, src_path):
        self.__src_path = src_path
        self.__event_handler = ImagesEventHandler()
        self.__event_observer = Observer()


    def run(self):
        self.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            self.stop()

    def start(self):
        self.__schedule()
        self.__event_observer.start()

    def stop(self):
        self.__event_observer.stop()
        self.__event_observer.join()

    def __schedule(self):
        self.__event_observer.schedule(
            self.__event_handler,
            self.__src_path,
            recursive=True
        )

if __name__ == "__main__":
    src_path = sys.argv[1] if len(sys.argv) > 1 else '.'
    ImagesWatcher(src_path).run()

imagesEventHandler.py
from watchdog.events import RegexMatchingEventHandler

class ImagesEventHandler(RegexMatchingEventHandler):
    THUMBNAIL_SIZE = (128, 128)
    IMAGES_REGEX = [r".*[^_thumbnail]\.jpg$"]

    def __init__(self):
        # self.Analyzer = Analyzer()
        super().__init__(self.IMAGES_REGEX)

    def on_created(self, event):
        self.process(event)

    def process(self, event):
        print("DETECTED")

Dockerfile:
FROM python:3.6
RUN pip install watchdog
ADD . .
WORKDIR /app/
RUN main.py

最佳答案

一位同事向我推荐了以下内容:https://github.com/merofeev/docker-windows-volume-watcher

关于python - 使用卷挂载的Docker上的事件监听器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787622/

相关文章:

javascript - eventListener 无法读取 keyCode

java - 如何处理 Java 事件队列

c# - 事件发生在读取和清除操作之间,因此,最后的数据将丢失

python - 在python中将文件下载为字符串

python - 从邻接矩阵中绘制具有边权重的特定网络

shell - 如何从 Windows shell 访问 docker VM (MobyLinux) 文件系统?

docker compose 卷类型 - 绑定(bind)与卷

python - numpy:切掉 2 列

python - 使用 pandas 高效计算时间特征

macos - macOS 中卷的 docker 中的相对路径绑定(bind)失败