test if a file is locked?

Fortepianissimo fortepianissimo at yahoo.com.tw
Sun Dec 21 20:24:24 EST 2003


I can lock a file using the following snippet:

---
import fcntl

f=open('flock.txt','w')
fcntl.lockf(f.fileno(),fcntl.LOCK_EX)
---

If some other script tries to open 'flock.txt', it'll wait until the
file is unlocked/closed.

My question is, I'd like to tell a script to quit *immediately* if it
cannot open the file, or, if it knows the file is locked.

The reason I need this is I want to serialize parallel processes using
a lock file. Any suggestion is welcome!




More information about the Python-list mailing list