title: fcntl file lock example not working
body: <p>I'm trying to workout a toy example of locking files in python 3.4.3, however I'm not getting the expected results.</p>
  
  <p>I have two scripts, script1.py, and script2.py:</p>
  
  <pre><code>#script1.py
  import pickle
  import pandas as pd
  import numpy as np
  import time
  import fcntl
  
  df = pd.DataFrame.from_dict({"script_id": [0], "val1": [0], "val2": [0]})
  df.to_pickle("data.pkl")
  
  for i in range(500):
      f = open("data.pkl", "rb+")
      while True:
          try:
              # lock if unlocked
              fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
              break
          except:
              time.sleep(0.01)
      df.loc[i, :] = np.concatenate([np.array([1]), np.random.sample(2)])
      time.sleep(np.random.uniform(0, 0.05))
      pickle.dump(df, f)
      # unlock when done
      fcntl.flock(f, fcntl.LOCK_UN)
      f.close()
  </code></pre>
  
  <p>Second script is very similar:</p>
  
  <pre><code>import pickle
  import pandas as pd
  import numpy as np
  import time
  import fcntl
  
  f = open("data.pkl", "rb")
  while True:
      try:
          fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
          df = pickle.load(f)
          fcntl.flock(f, fcntl.LOCK_UN)
          f.close()
          break
      except:
          time.sleep(0.001)
  
  for i in range(500, 1000):
      f = open("data.pkl", "rb+")
      while True:
          try:
              # lock if unlocked
              fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
              break
          except:
              time.sleep(0.01)
      df.loc[i, :] = np.concatenate([np.array([2]), np.random.sample(2)])
      time.sleep(np.random.uniform(0, 0.05))
      pickle.dump(df, f)
      # unlock when done
      fcntl.flock(f, fcntl.LOCK_UN)
      f.close()
  </code></pre>
  
  <p>The idea is that two scripts should read and write to a same file, with some artificial delays. </p>
  
  <p>Each script adds one random row to a dataframe loaded from data.pkl it should sum up to 1000 rows.</p>
  
  <p>I first run <code>script1</code>, then as fast as I can I run <code>script2</code>. I end up with <code>500 + n</code> rows dataframe where <code>n</code> is the number of appended rows before I run script2.</p>
  
  <p>Why I use pandas and numpy in a toy example? I will have a similar usage case, so I want to make sure it works with objects I'll be using.</p>