Search code examples
pythonwindowsntfs

Why does open("file", "w") not block?


I am on Windows using NTFS and I have 2 processes opening the same file in write mode (w). I was surprised to see, that both succeed. Am I missing someething? I thought write mode implicitly has a file-lock on the handle until the file is opened again (unless opened in a shared-write) mode?

Process 1:

fp = open("file.txt", "w")
time.sleep(10000)

Process 2:

fp = open("file.txt", "w")
time.sleep(10000)

Solution

  • Well, the implementation of your open() function must be passing FILE_SHARED_WRITE to the kernel. Otherwise, one of the calls would return an error, not block.