import time,random from multiprocessing import Process,Semaphore def ktv(i,sem): sem.acquire() print("{} come in horse".format(i)) time.sleep(random.randint(1,5)) print("{} go out horse".format(i)) sem.release() if __name__ == '__main__': sem = Semaphore(4) for i in range(20): p = Process(target=ktv,args=(i,sem)) p.start()