title: python inotify to monitor for In_closted_write and in_moved_to events
body: <p>I am monitoring a directory for new files to be moved to or created.
  Upon detecting the new file I call a another python script to process the file.</p>
  
  <pre><code>#!/usr/bin/python
  
  import os
  import signal
  import sys
  import logging
  import inotify.adapters
  import subprocess
  
  _DEFAULT_LOG_FORMAT = ''
  
  _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 exit_gracefully(signum, frame):
      signal.signal(signal.SIGINT, original_sigint)
      sys.exit(1)
  
      signal.signal(signal.SIGINT, exit_gracefully)
  
  def main():
      i = inotify.adapters.Inotify()
  
      i.add_watch(b'/home/sort/tmp')
  
      try:
          for event in i.event_gen():
              if event is not None:
                  if 'IN_MOVED_TO' in event[1] or 'IN_CLOSE_WRITE' in event[1]:
                      (header, type_names, watch_path, filename) = event
                      _LOGGER.info("%s" #"WD=(%d) MASK=(%d) COOKIE=(%d) LEN=(%d) MASK-&gt;NAMES=%s "
                           #"WATCH-PATH=[%s]"
                           "FILENAME=%s" + "/" + "%s",
                           type_names,#header.wd, header.mask, header.cookie, header.len, type_names,
                           watch_path.decode('utf-8'), filename.decode('utf-8'))
                  fnp = str(event[2] + "/" + event[3])
                  print fnp
                  proc = subprocess.Popen([orgpath, fnp], stderr=subprocess.STDOUT, bufsize=1)
                  #proc.communicate()
      finally:
          i.remove_watch(b'/home/sort/tmp')
  
  if __name__ == '__main__':
      _configure_logging()
  
      orgdir = os.path.dirname(os.path.realpath(sys.argv[0]))
      orgpath = os.path.join(orgdir, "organize.py")
  
      original_sigint = signal.getsignal(signal.SIGINT)
      signal.signal(signal.SIGINT, exit_gracefully)
      print("Watching /home/sort/tmp for new files")
      main()
  </code></pre>
  
  <p>The end goal is to only process one file at a time as I call to an API to scrape for metadata. To many calls to the API in a short period of time could result in the API key to be banned or temporarily blocked.</p>
  
  <p>Right now when I copy more than a single file into the monitoring directory the script gets called on each file at the same time.</p>