Watchdog: The watchdog with pygame

Created on 23 Nov 2018  路  5Comments  路  Source: gorakhargosh/watchdog

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()
not a bug

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:

#!/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()

All 5 comments

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alt3red picture alt3red  路  3Comments

SamSchott picture SamSchott  路  4Comments

bhargavrpatel picture bhargavrpatel  路  3Comments

datakurre picture datakurre  路  7Comments

hgrecco picture hgrecco  路  6Comments