代码太简单,原理不解释了。
def limitProc(limit=1):
exename=os.path.basename(__file__)
for inscnt in xrange(0,limit+1):
if inscnt>=limit: exit(0)
pidf="pid.%s.%d"%(exename, inscnt)
if os.path.exists(pidf):
with file(pidf) as fpid:
if os.path.exists('/proc/%s/stat'%(fpid.read())): continue
with file(pidf,"w") as fpid:
fpid.write(file('/proc/self/stat').read().split()[0])
break