python 多进程 信号Semaphore(并行编程 13)

2019-07-30 11:08:13 浏览数 (1)

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()

0 人点赞