dsoprea / PyInotify

An efficient and elegant inotify (Linux filesystem activity monitor) library for Python. Python 2 and 3 compatible.
GNU General Public License v2.0
245 stars 73 forks source link

parallelization of events notification #29

Closed MourIdri closed 7 years ago

MourIdri commented 7 years ago

Hi,

The Example provided works great to get notifications in a serial manner. However, it is not good for large images. For example, I am uploading a set of pictures. Pictures that are 20MB each and it takes 2 seconds each time to finish the notifications for each files. Then it goes to next file and so on, till all the of the upload. I tried to add parallelization of events notification without success : here is my code :

` import logging import threading import inotify.adapters

 _DEFAULT_LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'

 _LOGGER = logging.getLogger(__name__)

 def _configure_logging():
     _LOGGER.setLevel(logging.DEBUG)

     ch = logging.StreamHandler()

     formatter = logging.Formatter(_DEFAULT_LOG_FORMAT)
     ch.setFormatter(formatter)

     _LOGGER.addHandler(ch)

 #def PopUpMessage (event):
 #    (header, type_names, watch_path, filename) = event
 #    _LOGGER.info("WD=(%d) MASK=(%d) COOKIE=(%d) LEN=(%d) MASK->NAMES=%s "
 #        "WATCH-PATH=[%s] FILENAME=[%s]",
 #        header.wd, header.mask, header.cookie, header.len, type_names,
 #        watch_path.decode('utf-8'), filename.decode('utf-8'))

 def PopUpMessage (event):
     if event is not None:
         (header, type_names, watch_path, filename) = event
         _LOGGER.info("WD=(%d) MASK=(%d) COOKIE=(%d) LEN=(%d) MASK->NAMES=%s "
             "WATCH-PATH=[%s] FILENAME=[%s]",
             header.wd, header.mask, header.cookie, header.len, type_names,
             watch_path.decode('utf-8'), filename.decode('utf-8'))

 def _main():
     i = inotify.adapters.Inotify()

     i.add_watch(b'/PARA')

     try:
         threads = []
         while True: 
             for event in i.event_gen():

                 #if event is not None:
                 #(header, type_names, watch_path, filename) = event
                 #thread = threading.Thread
                 ti= threading.Thread(target=PopUpMessage(event))
                 threads += [ti]
                 ti.start()
             for x in threads:
                 x.join()

     finally:
         i.remove_watch(b'/PARA')

 if __name__ == '__main__':
     _configure_logging()
     _main()`