#!/usr/bin/env python
# encoding: utf-8 # Date: 2018/6/17# 多个进程共享一个服务,共享带来了竞争,竞争带来了混乱# 互斥锁就是把并发变成了串行,牺牲了效率,保证了数据不错乱from multiprocessing import Process, Lockimport timedef task(name, mutex): mutex.acquire() print('%s 1' % name) time.sleep(1) print('%s 2' % name) time.sleep(1) print('%s 3' % name) mutex.release() # 释放锁if __name__ == '__main__': mutex = Lock() # 要保证所有进程使用同一把锁,要把这传给子进程 for i in range(3): p = Process(target=task, args=('进程 % s' % i, mutex)) p.start()