new_contents: import fcntl
  import os
  
  
  class PidFile(object):
      def __init__(self, settings):
          self.path = settings['pid']
          self.pidfile = None
          self.enter()
  
      def enter(self):
          self.pidfile = open(self.path, "a+")
          try:
              fcntl.flock(self.pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
          except IOError:
              raise SystemExit("Already running according to " + self.path)
          self.pidfile.seek(0)
          self.pidfile.truncate()
          self.pidfile.write(str(os.getpid()))
          self.pidfile.flush()
          self.pidfile.seek(0)
          return self.pidfile
  
      def __exit__(self, exc_type=None, exc_value=None, exc_tb=None):
          try:
              self.pidfile.close()
          except IOError as err:
              # ok if file was just closed elsewhere
              if err.errno != 9:
                  raise
          os.remove(self.path)
  
subject: Create pidfile.py