Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fcntl, FCNTL
- import os, time
- FILE = "counter.txt"
- if not os.path.exists(FILE):
- # create the counter file if it doesn't exist
- file = open(FILE, "w")
- file.write("0")
- file.close()
- for i in range(20):
- # increment the counter
- file = open(FILE, "r+")
- fcntl.flock(file.fileno(), FCNTL.LOCK_EX)
- counter = int(file.readline()) + 1
- file.seek(0)
- file.write(str(counter))
- file.close() # unlocks the file
- print os.getpid(), "=>", counter
- time.sleep(0.1)
- ## 30940 => 1
- ## 30942 => 2
- ## 30941 => 3
- ## 30940 => 4
- ## 30941 => 5
- ## 30942 => 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement