testing if another instance of a script is already running

S

Strato

Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

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.

Any idea ?

Best regards,
Strato
 
L

Larry Bates

Strato said:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

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.

Any idea ?

Best regards,
Strato

Here is a recipe for Linux version of singleinstance class:

http://code.activestate.com/recipes/546512/

and I wrote and contributed the equivalent Windows version:

http://code.activestate.com/recipes/474070/

Hope this helps.

-Larry
 
L

Larry Bates

Strato said:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

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.

Any idea ?

Best regards,
Strato

Here is a recipe for Windows version of singleinstance class:

http://code.activestate.com/recipes/474070/

and I wrote and contributed the equivalent Linux version:

http://code.activestate.com/recipes/546512/

Hope this helps.

-Larry
 
A

Aaron \Castironpi\ Brady

Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

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.

Any idea ?

Best regards,
Strato

You could use msvcrt.locking, and just lock the script file. I am not
sure about this.
 
A

Aaron \Castironpi\ Brady

    >> 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
instead:

   http://pypi.python.org/pypi/lockfile

Skip

Would it suffice to call 'os.open' with flags= _O_CREAT| _O_EXCL ?
Would that be platform-independent?

Windows docs (_open):

_O_CREAT| _O_EXCL
Returns an error value if the file specified by filename exists.
Applies only when used with _O_CREAT.

man page:

O_EXCL
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.

CreateDirectory:

The CreateDirectory function does seem to guarantee this as well,
though is not as explicit:

Possible errors include the following.
ERROR_ALREADY_EXISTS
The specified directory already exists.

But the lock operation wouldn't need the 'if hasattr(os, "link")' test.
 
S

skip

Aaron> Would it suffice to call 'os.open' with flags= _O_CREAT| _O_EXCL
Aaron> ? Would that be platform-independent?

I suspect it would be platform-independent but not NFS-safe. (The other
solutions in lockfile might have NFS problems as well. In any case,
lockfile provides a bit more functionality as well, including timeouts and
the ability to break locks.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top