[IronPython] How to lock a file like win32file.LockFileEx()?

Sanghyeon Seo sanxiyn at gmail.com
Wed Nov 22 11:52:55 CET 2006

2006/11/21, Patrick O'Brien <sum.ergo.code at gmail.com>:
> What would one use in IronPython to get the equivalent of fcntl.flock() or
> win32file.LockFileEx()?

.NET's FileStream has Lock() and Unlock() methods, but I don't know
how to apply these to IronPython file object. Any idea?

One could use P/Invoke too.

Example IronPython code:

from System.IO import FileStream, FileMode
f = FileStream('test', FileMode.Open)
f.Lock(0, f.Length)
f.Unlock(0, f.Length)

Above is roughly equivalent to following CPython/Unix code:

import fcntl
f = open('test', 'w')
fcntl.lockf(f, fcntl.LOCK_EX|fcntl.LOCK_NB)
fcntl.lockf(f, fcntl.LOCK_UN)

Seo Sanghyeon

More information about the Ironpython-users mailing list