title: I need to prevent a python script from running twice
body: <p>I need to prevent a python script from running more than once. So far I have: </p>
  
  <pre><code>import fcntl
  def lockFile(lockfile):
      fp = open(lockfile, 'w')
      try:
          fcntl.flock(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
      except IOError:
          return False
  
      return True
  
  if not lockFile("myfile.lock"):
      sys.exit(0)
  </code></pre>
  
  <p>Problem is the sys.exit() never gets called even if the file is there. Maybe this is a platform dependent way of doing things? I just need to write a lockfile, check for its existence and if it's not there or stale, create a new one. Ideas?</p>