multiple instance on Unix

Batista, Facundo FBatista at uniFON.com.ar
Wed Sep 29 19:52:06 CEST 2004


[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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040929/5c0ff2c1/attachment.html>


More information about the Python-list mailing list