# File: msvcrt-example-3.py import msvcrt import os, time LK_UNLCK = 0 # unlock the file region LK_LOCK = 1 # lock the file region LK_NBLCK = 2 # non-blocking lock LK_RLCK = 3 # lock for writing LK_NBRLCK = 4 # non-blocking lock for writing FILE = "counter.txt" if not os.path.exists(FILE): file = open(FILE, "w") file.write("0") file.close() for i in range(20): file = open(FILE, "r+") # lock from current position (0) to end of file msvcrt.locking(file.fileno(), LK_LOCK, os.path.getsize(FILE)) 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)