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

时间:2019-06-27 09:06:37

标签: 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中运行代码时,eventlistener正在运行。

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

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

1 个答案:

答案 0 :(得分:0)

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