Python锁

  

# coding:utf-8
import threading
import time


def test_xc():
    f = open("test.txt","a")
    f.write("test_dxc"+'\n')
    time.sleep(1)
    mutex.acquire()#取得锁
    f.close()
    mutex.release()#释放锁

if __name__ == '__main__':
    mutex = threading.Lock()#创建锁
    for i in xrange(5):
        t = threading.Thread(target=test_xc)
        t.start()

 

posted @ 2017-06-03 19:43  413Xiaol  阅读(117)  评论(0编辑  收藏  举报