Re: __del__ pattern?



Bryan Olson wrote:
> > Use file that is writeable by A and B in a directory that is
> > writeable only by root.
>
> Is that portable?

I have the feeling that you are asking if it works on Windows.
No idea! I have only user experience with Windows.

On UNIX it is as portable as 'flock', which means all modern
Unices (be careful about NFS).

> What's the sequence the program should try?

1.
open a file, which name was previously agreed on
(like /var/tmp/<prog-name>-<user-name>)

If it fails, report error and exit. System error or
somebody has created unaccessible file by the same name.

2.
Try to aquire a flock on the descriptor from step 1.

If it fails, some running process already has the lock, exit

3.
lock will be released and lockfile closed automaticaly by OS
on process exit.

import sys, fcntl

try:
lockfile=open('/var/tmp/test1', 'w')
fcntl.flock(lockfile.fileno(),
fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
print sys.exc_info()[1]
sys.exit(-1)

You can flock any open file, no matter if it is read/write/append.

BranoZ

.



Relevant Pages

  • Re: Table is in used
    ... IF FLOCK() && Trying to lock this file ... EXIT && nobody is using it so we can exit the loop ...
    (microsoft.public.fox.helpwanted)
  • Re: I cant flock, it returns 0, any ideas.
    ... > I can't flock, it returns 0, any ideas. ... You can only hold a lock on an open file. ... will fail but attempts to get another LOCK_SH lock will ... this is not how you pause the script! ...
    (comp.lang.perl.misc)
  • Re: I cant flock, it returns 0, any ideas.
    ... # returns zero when I try to flock ... >> I wanted to run this script to see if it would affect my other perl ... If someone holds a LOCK_EX lock, all attempts to get a lock will ... > the flock call will fail, and if you don't flock will just not ...
    (comp.lang.perl.misc)
  • Re: UW imap-2006b: 64 bit problem
    ... UW imapd is obliged> to do considerable more work on SVR4 systems than it does on BSD and> Linux systems which offer flock() locking as an alternative to POSIX> locking. ... IEEE Std 1003.1-1988 requires that all fcntl() locks associated with a file for a given process are removed when *any* file descriptor for that file is closed by that process. ... Put another way, before any library routine opens a file, it must be aware of what files the application and any other libraries have open and locked; otherwise the library routine will remove the lock unexpectedly when it closes the file. ...
    (comp.mail.imap)
  • Re: flock() replacement?
    ... lock/unlock library code. ... It uses flock() to synchronize ... pthread mutex lock. ... linux 2.6, flockconsumes the most user and sys times. ...
    (comp.unix.programmer)