python 執行緒互斥鎖用法 簡單案例 threading.Lock()
阿新 • • 發佈:2019-01-22
環境python2.7# encoding: UTF-8 import threading import time # # 建立鎖 # lock=threading.Lock() # # 鎖定 # lock.acquire() # # 釋放 # lock.release() def test_xc(num): f = open("test.txt", "a") f.write(str(num) + '\n') time.sleep(1) lock.acquire() # 取得鎖 f.close() lock.release() # 釋放鎖 if __name__ == '__main__': lock = threading.Lock() # 建立鎖 for i in xrange(5): t = threading.Thread(target=test_xc,args=('abc',)) t.start()