file
  • parallel-processing
  • python-multithreading
  • inotify
  • 2017-02-23 7 views 0 likes 
    0

    디렉토리 (여기서는 tmp)에 파일이나 폴더가 생성 될 때 "Inotify"를 사용하여 이벤트를 기록합니다. 여기의 예는 일을 직렬 프로세스로 처리합니다. 의미, 모든 파일 생성은 하나씩 순차적으로 처리됩니다.파일 생성시 이벤트 통보의 병렬화

    import logging 
    
    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 _main(): 
        i = inotify.adapters.Inotify() 
    
        i.add_watch(b'/tmp') 
    
        try: 
         for event in i.event_gen(): 
          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')) 
        finally: 
         i.remove_watch(b'/tmp') 
    
    if __name__ == '__main__': 
        _configure_logging() 
        _main() 
    
    내가 스레딩을 가져 업로드 여러 파일의 경우 이벤트 알림의 병렬화를 소개하고 싶습니다

    , 나는 루프와 스레딩을 추가해야 ? 두 번째 관심사는 thread 함수를 넣는 것이 어디에서 가능할지 모르겠습니다.

    답변

    -1

    아래 스크립트는 배수 세션의 경우 배수 이벤트를 처리합니다. 그래서 제 경우에는 충분합니다. 스레딩 대신 멀티 프로세싱 옵션을 추가했습니다. 스레딩보다 멀티 프로세싱 속도가 빠르다.

    import logging 
    import threading 
    import inotify.adapters 
    import multiprocessing 
    
    _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): 
        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 My_main(count): 
        i = inotify.adapters.Inotify() 
        DirWatcher=i.add_watch(b'/PARA') 
        try: 
         while True: 
          for event in i.event_gen(): 
           m = multiprocessing.Process(target=PopUpMessage, args=(event,)) 
           m.start()    
    
        finally: 
         i.remove_watch(b'/PARA') 
    
    if __name__ == '__main__': 
        _configure_logging() 
        N = multiprocessing.Process(target=My_main) 
        N.start() 
    

     관련 문제

    • 관련 문제 없음^_^