from multiprocessing import Semaphore,Process import time import random
def ktv(sem,i): with sem: print(f'{i}走进ktv') time.sleep(random.randint(1,3)) print(f'{i}走出ktv')
if name == 'main': sem = Semaphore(4) for i in range(10): p = Process(target=ktv,args=(sem,i)) p.start()