python中限制进程实例个数实现

来源:互联网 发布:淘宝店铺扣分在哪里看 编辑:程序博客网 时间:2024/05/21 11:19

代码太简单,原理不解释了。

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


0 0
原创粉丝点击