python(lock)

# Author: Sooele

from multiprocessing import Process, Lock  #从来multiprocessing导入Process,Lock


def f(l, i):
    l.acquire()
    try:
        print('hello world', i)
    finally:
        l.release()


if __name__ == '__main__':
    lock = Lock()  #生成锁的实例

    for num in range(10):  #启动10个线程
        Process(target=f, args=(lock, num)).start()

发表评论