multiple instance on Unix

Jeremy Jones zanesdad at bellsouth.net
Wed Sep 29 14:14:31 EDT 2004


Batista, Facundo wrote:

> [Nigel King]
>
> #- I have (my son has!) implemented protection against multiple
> #- instances
> #- causing havoc by creating a directory. This fails if it
> #- exists and thus
> #- in a single instruction one gets both the acquire and the test.
>
> Multiple instances of objects? Multiple instances of processes?
>
> I needed to not have the same process twice and wrote the following:
>
>
> def controlSimult(coderr=-1):
>         """Function that verifies that there's not other process (of 
> itself) already in memory."""
>
>         # do a ps
>         (stdin, stdout) = os.popen4('ps -eaf')
>         ps = stdout.readlines()
>
>         # search instances of ourselves
>         abuscar = sys.executable + ' ' + sys.argv[0]
>         coinc = [x[:-1] for x in ps if x.find(abuscar) != -1]
>
>         # there's more than us-now?
>         if len(coinc) > 1:
>                 print "There're more simultaneously in memory:"
>                 # show only that are not us
>                 ownpid = os.getpid()
>                 print '\n'.join([x for x in coinc if int(x.split()[1]) 
> != ownpid])
>                 sys.exit(coderr)
>         return
>
> There's a better way?
>
> .       Facundo
>
In *NIX systems, can't you alias how process names show up in the 
process table?  I'm not sure if processes are self-aware of what they 
really are, or if they are aware of only the alias, you could also use a 
lock file. Something like:

    * Upon starting the process, look to see that the lock file does not
      exist - if it does, exit and log an error. (If the file exists, it
      means either another instance of the process is already running,
      or one was and it uncleanly shut down.)
    * Write your own pid to the lock file you were looking for in the
      previous step.  Only problem here is how to open the file in write
      mode and fail if the file already exists (for pathological cases
      where you first check for the file and another process checks at
      nearly the same time, and one of the processes opens the file in
      write mode after the other process checks for it and before the
      other process itself opens the file in write mode - wow, what a
      runon sentence).
    * Do stuff
    * Delete the file when you're done

Yeah - it's got problems, but so does nearly any other way of making 
sure two processes don't step on each other.  Maybe a lock directory is 
a good way of doing it after all.  Maybe something like the code I just 
posted:

import time
import os
waiting_for_lock = 1

while waiting_for_lock:
    try:
        os.mkdir('/tmp/foo')
        waiting_for_lock = 0
    except OSError:
        print "could not create directory"
        time.sleep(1)

Maybe even write your pid in a file in the directory once you are past 
the while loop.  Having the interpreter throw an exception when you try 
to do something like create a directory could come in handy in such a 
case as this.


Jeremy Jones

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040929/5a7cf8da/attachment.html>


More information about the Python-list mailing list