Development

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Allowing only one instance of a script?

    4 answers - 328 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,
    I have a script which I double-click to run. If i double-click it
    again, it will launch another instance of the script.
    Is there a way to allow only one instance of a script, so that if
    another instance of the script is launched, it will just return with an
    error.
    Thanks
    Regards,
    Ali
  • No.1 | | 827 bytes | |

    Am Wed, 22 Jun 2005 23:49:21 -0700 schrieb Ali:

    Hi,

    I have a script which I double-click to run. If i double-click it
    again, it will launch another instance of the script.

    Is there a way to allow only one instance of a script, so that if
    another instance of the script is launched, it will just return with an
    error.

    Hi,

    Create a file which contains the PID (process ID) of
    the current process in a directory. If the file
    already exists, the file is running.

    If your script dies without removing the pid-file, you
    need to look during the start if the PID which is in the
    file is sill alive.

    There is a small race condition between os.path.exists()
    and writing the file. If you want to be 100% sure you need
    to use file locking.

    HTH,
    Thomas
  • No.2 | | 1057 bytes | |

    2005-06-23, Thomas Guettler <guettli (AT) thomas-guettler (DOT) dewrote:

    Create a file which contains the PID (process ID) of
    the current process in a directory. If the file
    already exists, the file is running.

    That's how it's usually done.

    If your script dies without removing the pid-file, you need to
    look during the start if the PID which is in the file is sill
    alive.

    There is a small race condition between os.path.exists()
    and writing the file.

    That's why it's pointless to call os.path.exists().

    If you want to be 100% sure you need to use file locking.

    I've never seen it done that way.

    The standard method is to use open() with flags CREAT|EXCL.
    If the open() is sucessful, then you have the lock. If it
    fails, somebody else already has the lock.

    Another method is to create a temp file containing the PID and
    then call link() to rename it.

    Both open() and link() are atomic operations, so there's no
    race condition.
  • No.3 | | 293 bytes | |

    In article <11bljh588m0r89c (AT) corp (DOT) supernews.com>,
    Grant Edwards <grante (AT) visi (DOT) comwrote:
    >
    >Both open() and link() are atomic operations, so there's no
    >race condition.

    unless you're running under NFS.
  • No.4 | | 559 bytes | |

    2005-06-23, Aahz <aahz (AT) pythoncraft (DOT) comwrote:
    In article <11bljh588m0r89c (AT) corp (DOT) supernews.com>,
    Grant Edwards <grante (AT) visi (DOT) comwrote:
    >>
    >>Both open() and link() are atomic operations, so there's no
    >>race condition.

    >

    unless you're running under NFS.

    I think read somewhere than NFS 3 handles the open with
    CREAT+EXCL in an atomic manner (older versions didn't). I don't
    know about link.

Re: Allowing only one instance of a script?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 6 + 5 =
QUESTION ON "Development"

EMSDN.COM