test if a file is locked?
Carl Banks
imbosol at aerojockey.invalid
Mon Dec 22 02:02:50 EST 2003
Fortepianissimo wrote:
>
>
> 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!
Try this:
fcntl.lockf(f.fileno(),fcntl.LOCK_EX|fcntl.LOCK_NB)
--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
More information about the Python-list
mailing list