I would like to use the watchdog with pygame, is this possible? The purpose of my script is that when something happens in the folder a video will be displayed. The problem is that when I run the script it does not return any errors.
Here's the code I'm using.
#!/usr/bin/python
import time
import os
import pygame
from moviepy.editor import *
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
link = 'abs\\'
def detectFile():
pygame.display.set_caption('Hello World!')
clip = VideoFileClip('01.mp4')
clip.preview(fps=30,fullscreen=True)
pygame.quit()
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print 'test'
detectFile()
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=link, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
@alciomarhollanda please, can you provide what OS you are using? Your script and 'abs\' located in same directory?
Hello @rrzaripov , I use windows 10. The folder abs\ is in same directory.
Thanks
@alciomarhollanda this is not watchdog issue. and you can close this issue. I was tested your code and verify that watchdog detect new file creation in directory abs\ and print 'test'. But problem in where you work with pygame. You create handler and pass this to Observer instance. Actually Observer instance work as separate thread. And how I know, pygame work in main thread only. So when Observer instance calling handler (and function detectFile) pygame not work, because this is done not in main thread. Here working example:
#!/usr/bin/python
import time
import os
import pygame
from moviepy.editor import *
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from threading import Event
modified = Event()
link = 'abs\\'
def detectFile():
while True:
modified.wait()
pygame.display.set_caption('Hello World!')
clip = VideoFileClip('01.mp4')
clip.preview(fps=30,fullscreen=True)
pygame.quit()
break
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print 'test'
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=link, recursive=False)
observer.start()
detectFile()
observer.stop()
observer.join()
@BoboTiG this issue can be closed.
@BoboTiG este problema pode ser fechado.
thanks you @BoboTiG
Most helpful comment
@alciomarhollanda this is not watchdog issue. and you can close this issue. I was tested your code and verify that watchdog detect new file creation in directory abs\ and print 'test'. But problem in where you work with pygame. You create handler and pass this to Observer instance. Actually Observer instance work as separate thread. And how I know, pygame work in main thread only. So when Observer instance calling handler (and function detectFile) pygame not work, because this is done not in main thread. Here working example: