# File: fcntl-example-1.py 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)