multiple instance on Unix
FBatista at uniFON.com.ar
Wed Sep 29 19:52:06 CEST 2004
#- I have (my son has!) implemented protection against multiple
#- 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:
"""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
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()) !=
There's a better way?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list