进程的同步

# 进程的同步 (类似于线程的同步锁,同一时刻只能一个进程运行)
from multiprocessing import Process, Lock
import time

def f(l, i):
    with l:
        time.sleep(1)
        print('helloworld %s' % i)

    # 上面with l: 相当于
    # l.acquire()
    #     print('helloworld %s' % i)
    # l.release()

if __name__ == '__main__':
    lock = Lock()
    for num in range(10):
        Process(target=f, args=(lock, num,)).start()

 

posted @ 2018-08-19 00:03  四十不惑的编程之路  阅读(148)  评论(0编辑  收藏  举报