<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Batista, Facundo wrote:<br>
<blockquote
 cite="midA128D751272CD411BC9200508BC2194D053C7A06@escpl.tcp.com.ar"
 type="cite">
  <meta http-equiv="Content-Type" content="text/html; ">
  <meta name="Generator" content="MS Exchange Server version 5.5.2658.2">
  <title>RE: multiple instance on Unix</title>
  <p><font size="2">[Nigel King]</font>
  </p>
  <p><font size="2">#- I have (my son has!) implemented protection
against multiple </font>
  <br>
  <font size="2">#- instances </font>
  <br>
  <font size="2">#- causing havoc by creating a directory. This fails
if it </font>
  <br>
  <font size="2">#- exists and thus </font>
  <br>
  <font size="2">#- in a single instruction one gets both the acquire
and the test.</font>
  </p>
  <p><font size="2">Multiple instances of objects? Multiple instances
of processes?</font>
  </p>
  <p><font size="2">I needed to not have the same process twice and
wrote the following:</font>
  </p>
  <br>
  <p><font size="2">def controlSimult(coderr=-1):</font>
  <br>
  <font size="2">        """Function that verifies that there's not
other process (of itself) already in memory."""</font>
  </p>
  <p><font size="2">        # do a ps</font>
  <br>
  <font size="2">        (stdin, stdout) = os.popen4('ps -eaf')</font>
  <br>
  <font size="2">        ps = stdout.readlines()</font>
  </p>
  <p><font size="2">        # search instances of ourselves</font>
  <br>
  <font size="2">        abuscar = sys.executable + ' ' + sys.argv[0]</font>
  <br>
  <font size="2">        coinc = [x[:-1] for x in ps if x.find(abuscar)
!= -1]</font>
  </p>
  <p><font size="2">        # there's more than us-now?</font>
  <br>
  <font size="2">        if len(coinc) > 1:</font>
  <br>
  <font size="2">                print "There're more simultaneously in
memory:"</font>
  <br>
  <font size="2">                # show only that are not us</font>
  <br>
  <font size="2">                ownpid = os.getpid()</font>
  <br>
  <font size="2">                print '\n'.join([x for x in coinc if
int(x.split()[1]) != ownpid])</font>
  <br>
  <font size="2">                sys.exit(coderr)</font>
  <br>
  <font size="2">        return</font>
  </p>
  <p><font size="2">There's a better way?</font>
  </p>
  <p><font size="2">.       Facundo</font>
  </p>
</blockquote>
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:<br>
<br>
<ul>
  <li>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.)<br>
  </li>
  <li>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).</li>
  <li>Do stuff</li>
  <li>Delete the file when you're done</li>
</ul>
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:<br>
<br>
import time<br>
import os<br>
waiting_for_lock = 1<br>
<br>
while waiting_for_lock:<br>
    try:<br>
        os.mkdir('/tmp/foo')<br>
        waiting_for_lock = 0<br>
    except OSError:<br>
        print "could not create directory"<br>
        time.sleep(1)<br>
<br>
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.<br>
<br>
<br>
Jeremy Jones<br>
<br>
</body>
</html>