Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. import fcntl, FCNTL
  2. import os, time
  3.  
  4. FILE = "counter.txt"
  5.  
  6. if not os.path.exists(FILE):
  7. # create the counter file if it doesn't exist
  8. file = open(FILE, "w")
  9. file.write("0")
  10. file.close()
  11.  
  12. for i in range(20):
  13. # increment the counter
  14. file = open(FILE, "r+")
  15. fcntl.flock(file.fileno(), FCNTL.LOCK_EX)
  16. counter = int(file.readline()) + 1
  17. file.seek(0)
  18. file.write(str(counter))
  19. file.close() # unlocks the file
  20. print os.getpid(), "=>", counter
  21. time.sleep(0.1)
  22.  
  23. ## 30940 => 1
  24. ## 30942 => 2
  25. ## 30941 => 3
  26. ## 30940 => 4
  27. ## 30941 => 5
  28. ## 30942 => 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement