Search within running python scripts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gmax2006

    #1

    Search within running python scripts

    Hi,

    Is it possible that a python script finds out whether another instance
    of it is currently running or not?

    Thank you,
    Max

  • Simon Forman

    #2
    Re: Search within running python scripts

    gmax2006 wrote:
    Hi,
    >
    Is it possible that a python script finds out whether another instance
    of it is currently running or not?
    >
    Thank you,
    Max
    Yes, there are several ways. What OS are you using?

    ~Simon

    Comment

    • faulkner

      #3
      Re: Search within running python scripts

      IPC via files, sockets, and shared memory are all readily available in
      python.
      the simplest way is to have the script write its pid to a certain file.

      pidfn = '/tmp/hellowerld_ipc_ pid'
      if os.path.isfile( pidfn):
      f = file(pidfn)
      pid = f.read()
      f.close()
      if pid in os.popen('ps -A -o pid').read():
      print "another instance of me is running!"
      else:
      f = file(pidfn, 'w')
      f.write(str(os. getpid()))
      f.close()


      gmax2006 wrote:
      Hi,
      >
      Is it possible that a python script finds out whether another instance
      of it is currently running or not?
      >
      Thank you,
      Max

      Comment

      • gmax2006

        #4
        Re: Search within running python scripts


        Simon Forman wrote:
        gmax2006 wrote:
        Hi,

        Is it possible that a python script finds out whether another instance
        of it is currently running or not?

        Thank you,
        Max
        >
        Yes, there are several ways. What OS are you using?
        >
        ~Simon
        I have to use an os-independent approach.

        At this point I use a file as running-flag. It doesn't work so good.
        Because if the python application breaks or get terminated, it won't
        run again unless somebody deletes the flag file.

        Alan

        Comment

        • Simon Forman

          #5
          Re: Search within running python scripts

          gmax2006 wrote:
          Simon Forman wrote:
          gmax2006 wrote:
          Hi,
          >
          Is it possible that a python script finds out whether another instance
          of it is currently running or not?
          >
          Thank you,
          Max
          Yes, there are several ways. What OS are you using?

          ~Simon
          >
          I have to use an os-independent approach.
          >
          At this point I use a file as running-flag. It doesn't work so good.
          Because if the python application breaks or get terminated, it won't
          run again unless somebody deletes the flag file.
          >
          Alan
          Hmm, I'm very far from being an expert on this, so hopefully someone
          who knows better will reply.
          You might have to check the OS your script is running on and do, say,
          what faulkner proposed for linux (and Mac?), and something like
          http://aspn.activestate.com/ASPN/Coo.../Recipe/474070 for
          windows.

          HTH,
          ~Simon

          Comment

          • Cameron Laird

            #6
            Re: Search within running python scripts

            In article <1153778084.307 580.192950@p79g 2000cwp.googleg roups.com>,
            Simon Forman <rogue_pedro@ya hoo.comwrote:
            >gmax2006 wrote:

            Comment

            • Simon Forman

              #7
              Re: Search within running python scripts

              Cameron Laird wrote:
              ....
              Particularly when I hear "os-independent", I think first of
              binding to a socket. While <URL: http://wiki.tcl.tk/1558 >
              is written for a Tcl-based crowd, the commentary there ap-
              plies quite well to Python.
              I was going to suggest something like this, as I have noticed that IDLE
              seems to do exactly this, and on windows and linux, but I was afraid to
              look the fool if it was indeed foolish. (and also, I didn't know
              details of it.)

              Thanks Cameron.

              Peace,
              ~Simon

              Comment

              Working...