# coding:utf-8 import threading import time def test_xc(): f = open("test.txt","a") f.write("test_dxc"+' ') 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()