from time import sleep
from multiprocessing import Process, Lock
def time_delay(d):
sleep(d)
def printer(item, lock):
print(item)
lock.acquire()
print('time delay start')
time_delay(1)
print('time delay end')
lock.release()
if __name__ == '__main__':
lock = Lock()
items = ['item1', 'item2', 'item3']
for item in items:
p = Process(target=printer, args=(item, lock))
p.start()
请问各位前辈,为何输出结果是
item1
time delay start
time delay end
item2
time delay start
time delay end
item3
time delay start
time delay end
而不是
item1
item2
item3
time delay start
time delay end
time delay start
time delay end
time delay start
time delay end
明明print item的时候还没被Lock住阿...
而且他是先执行time_delay才开始print item...
请问是我哪里误会了lock的用法?
谢谢各位!