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

Discussion in 'Python' started by Dan Sommers, Dec 15, 2009.

  1. Dan Sommers

    Dan Sommers Guest

    On Mon, 14 Dec 2009 14:14:05 -0500, python 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.


    Maybe I'm missing something, but the locking mechanism already exists:
    at some point, your server program has to bind to an IP port to listen
    for incoming request, and any respectable OS won't let two programs bind
    to the same port at the same time. So if binding to the input port
    works, then there *can't* be another instance of the program running
    (multiple configuration files notwithstanding, but then you'd need a
    second process anyway).

    I guess I am assuming that "a server program based on cherrypy" takes its
    input from an IP port, but that seems safe enough given the nature of
    cherrypy.

    Dan
    Dan Sommers, Dec 15, 2009
    #1
    1. Advertising

  2. Dan Sommers

    Hans Mulder Guest

    Dan Sommers wrote:
    > On Mon, 14 Dec 2009 14:14:05 -0500, python 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.

    >
    > Maybe I'm missing something, but the locking mechanism already exists:
    > at some point, your server program has to bind to an IP port to listen
    > for incoming request, and any respectable OS won't let two programs bind
    > to the same port at the same time.


    Unfortunately, Windows is not a respectable OS. Unlike Unix, it allows
    two processes to bind to the same port. The theory is that this somehow
    allows the two processes to share their workload.

    One thing the OP can portably do, is try to connect() to the port.
    If that succeeds, then a server program is already running at that port,
    so he should exit.


    Hope this helps,

    -- HansM
    Hans Mulder, Dec 22, 2009
    #2
    1. Advertising

  3. Dan Sommers

    Guest

    Hans,

    > Unfortunately, Windows is not a respectable OS. Unlike Unix, it allows two processes to bind to the same port. The theory is that this somehow allows the two processes to share their workload. One thing the OP can portably do, is try to connect() to the port. If that succeeds, then a server program is already running at that port, so he should exit.


    Thank you for your tip - spot on!

    Best regards,
    Malcolm (OP for this thread)
    , Dec 28, 2009
    #3
  4. Dan Sommers

    Dan Sommers Guest

    On Sun, 27 Dec 2009 19:07:12 -0500, python wrote:

    > Hans,
    >
    >> Unfortunately, Windows is not a respectable OS. Unlike Unix, it allows
    >> two processes to bind to the same port. The theory is that this somehow
    >> allows the two processes to share their workload. One thing the OP can
    >> portably do, is try to connect() to the port. If that succeeds, then a
    >> server program is already running at that port, so he should exit.

    >
    > Thank you for your tip - spot on!


    Beware of the converse, though. If a process *cannot* connect to the
    port, then it should *not* assume that another server *isn't* running.
    If two potential servers both start at the same time, and each tries to
    connect, then both will fail, but you don't want both to start, either.
    Dan Sommers, Dec 28, 2009
    #4
  5. Dan Sommers

    Guest

    Dan,

    >> Unfortunately, Windows is not a respectable OS. Unlike Unix, it allows
    >> two processes to bind to the same port. The theory is that this somehow
    >> allows the two processes to share their workload. One thing the OP can
    >> portably do, is try to connect() to the port. If that succeeds, then a
    >> server program is already running at that port, so he should exit.


    > Beware of the converse, though. If a process *cannot* connect to the
    > port, then it should *not* assume that another server *isn't* running.
    > If two potential servers both start at the same time, and each tries to
    > connect, then both will fail, but you don't want both to start, either.


    Thank you for pointing out that nuance.

    Best regards,
    Malcolm
    , Dec 28, 2009
    #5
    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:
    414
    Joshua Flanagan
    Oct 26, 2003
  2. Craig Ringer
    Replies:
    1
    Views:
    399
    Mustafa Demirhan
    Nov 18, 2004
  3. Replies:
    0
    Views:
    276
  4. =?iso-8859-1?B?QW5kcuk=?=
    Replies:
    8
    Views:
    379
    =?iso-8859-1?B?QW5kcuk=?=
    May 1, 2007
  5. MRAB
    Replies:
    3
    Views:
    514
Loading...

Share This Page