生活随笔
收集整理的這篇文章主要介紹了
进程锁Lock
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#-*- encoding:utf-8 -*-
import multiprocessing
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def worker_with(lock, f):with lock:#這里給資源上鎖fs = open(f, 'a+')n = 3while n > 1:fs.write("Lockd acquired via withn\n")n -= 1fs.close()def worker_no_with(lock, f):lock.acquire()try:fs = open(f, 'a+')n = 3while n > 1:fs.write("Lock acquired directly\n")n -= 1fs.close()finally:lock.release()if __name__ == "__main__":lock = multiprocessing.Lock()f = "file.txt"w = multiprocessing.Process(target = worker_with, args=(lock, f))nw = multiprocessing.Process(target = worker_no_with, args=(lock, f))w.start()nw.start()print ("end")
總結(jié)
以上是生活随笔為你收集整理的进程锁Lock的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。