testing if another instance of a script is already running
Aaron "Castironpi" Brady
castironpi at gmail.com
Sun Sep 14 02:32:35 CEST 2008
On Sep 13, 5:34 pm, s... at pobox.com wrote:
> >> I don't want to handle writing of a PID file because it is too
> >> Unix/Linux specific way to do this, and I need to keep the code to be
> >> cross-platform.
> >> I think the better way to achieve this is to use some process
> >> control, but I'm a neebie and I don't see how to do this in a safe
> >> and clean way.
> Aaron> You could use msvcrt.locking, and just lock the script file. I
> Aaron> am not sure about this.
> If you want a cross-platform solution, you might try the lockfile module
Would it suffice to call 'os.open' with flags= _O_CREAT| _O_EXCL ?
Would that be platform-independent?
Windows docs (_open):
Returns an error value if the file specified by filename exists.
Applies only when used with _O_CREAT.
If O_EXCL and O_CREAT are set, open will fail if the file exists.
The check for the existence of the file and the creation of the file
if it does not exist is atomic with respect to other processes
executing open naming the same filename in the same directory with
O_EXCL and O_CREAT set.
The CreateDirectory function does seem to guarantee this as well,
though is not as explicit:
Possible errors include the following.
The specified directory already exists.
But the lock operation wouldn't need the 'if hasattr(os, "link")' test.
More information about the Python-list