Re: OS independent way to check if a python app is running?

Discussion in 'Python' started by MRAB, Dec 14, 2009.

  1. MRAB

    MRAB Guest

    wrote:
    > Is there an os independent way to check if a python app is running?
    >
    > Goal: I have a server program based on cherrypy that I only want to have
    > running once. If a system administrator accidentally attempts to run
    > this program more than once, I would like the 2nd instance of the
    > program to detect that its already running and exit.
    >

    You could use lockfile: http://pypi.python.org/pypi/lockfile/0.7

    If a certain file exists and is locked, then the app is already running.
     
    MRAB, Dec 14, 2009
    #1
    1. Advertising

  2. MRAB schrieb:
    > wrote:
    >> Is there an os independent way to check if a python app is running?
    >>
    >> Goal: I have a server program based on cherrypy that I only want to
    >> have running once. If a system administrator accidentally attempts to
    >> run this program more than once, I would like the 2nd instance of the
    >> program to detect that its already running and exit.
    >>

    > You could use lockfile: http://pypi.python.org/pypi/lockfile/0.7
    >
    > If a certain file exists and is locked, then the app is already running.


    Not only exists, he should also use the OS' locking mechanisms. The file
    could otherwise be stale.

    We use this:


    import platform
    is_windows = False
    if platform.system() == 'Windows':
    is_windows = True
    import os


    class LockFileCreationException(Exception):
    pass


    class LockFile(object):

    def __init__(self, name, fail_on_lock=False, cleanup=True):
    self.name = name
    self.cleanup = cleanup
    try:
    self.fd = os.open(name, os.O_WRONLY | os.O_CREAT | os.O_APPEND)
    except OSError, e:
    if e[0] == 2:
    raise LockFileCreationException()
    self.file = os.fdopen(self.fd, "w")
    if is_windows:
    lock_flags = msvcrt.LK_LOCK
    else:
    lock_flags = fcntl.LOCK_EX
    if fail_on_lock:
    if is_windows:
    lock_flags = msvcrt.LK_NBLCK
    else:
    lock_flags |= fcntl.LOCK_NB
    try:
    if is_windows:
    msvcrt.locking(self.file.fileno(), lock_flags, 1)
    else:
    fcntl.flock(self.file, lock_flags)
    except IOError, e:
    if e[0] == 11:
    raise LockObtainException()
    raise


    def __enter__(self):
    return self.file


    def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb):
    self.file.close()
    # we are told to cleanup after ourselves,
    # however it might be that another process
    # has done so - so we don't fail in that
    # case.
    if self.cleanup:
    try:
    os.remove(self.name)
    except OSError, e:
    if not e[0] == 2:
    raise



    Diez
     
    Diez B. Roggisch, Dec 14, 2009
    #2
    1. Advertising

  3. MRAB

    Guest

    Diez,

    Thank you for your sample code. That was just what we were looking for.

    Regards,
    Malcolm
     
    , Dec 14, 2009
    #3
  4. MRAB

    srid Guest

    On Dec 14, 12:35 pm, MRAB <> wrote:
    > wrote:
    > > Is there an os independent way to check if a python app is running?

    >
    > > Goal: I have a server program based on cherrypy that I only want to have
    > > running once. If a system administrator accidentally attempts to run
    > > this program more than once, I would like the 2nd instance of the
    > > program to detect that its already running and exit.

    >
    > You could use lockfile:http://pypi.python.org/pypi/lockfile/0.7
    >
    > If a certain file exists and is locked, then the app is already running.


    How is this different from the zc.lockfile package?
    http://pypi.python.org/pypi/zc.lockfile/

    -srid
     
    srid, Dec 28, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hagay Lupesko

    Running exe as an independent process

    Hagay Lupesko, Oct 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Joshua Flanagan
    Oct 26, 2003
  2. Craig Ringer
    Replies:
    1
    Views:
    400
    Mustafa Demirhan
    Nov 18, 2004
  3. Replies:
    0
    Views:
    277
  4. =?iso-8859-1?B?QW5kcuk=?=
    Replies:
    8
    Views:
    385
    =?iso-8859-1?B?QW5kcuk=?=
    May 1, 2007
  5. Dan Sommers
    Replies:
    4
    Views:
    1,033
Loading...

Share This Page