python多线程并控制并发数实现

made by G.M

import threading
import time
def printthread(n):
    print n,"---------------->进程创建"
    for a in range(4):
        print a
        time.sleep(1)
    print n,"-->进程结束"
    sem.release()
if __name__ == "__main__":
    maxThread=2
    sem=threading.BoundedSemaphore(maxThread)
    for a in  range(12):
        sem.acquire()
        i=threading.Thread(target=printthread,args=(a,))
        i.start()
    print "All thread has create,Wait for all thread exit."
    print "All thread exit"