国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python多線程同步---文件讀寫控制方法

2020-02-16 01:06:19
字體:
來源:轉載
供稿:網友

1、實現文件讀寫的文件ltz_schedule_times.py

#! /usr/bin/env python#coding=utf-8import osdef ReadTimes(): res = [] if os.path.exists('schedule_times.txt'):  fp = open('schedule_times.txt', 'r') else:  os.system('touch schedule_times.txt')  fp = open('schedule_times.txt', 'r') try:  line = fp.read()  if line == None or len(line)==0:   fp.close()   return 0  tmp = line.split()  print 'tmp: ', tmp  schedule_times = int(tmp[-1]) finally:  fp.close() #print schedule_times return schedule_timesdef WriteTimes(schedule_times): if schedule_times <= 10:  fp = open('schedule_times.txt', 'a+')#10以內追加進去 else:  fp = open('schedule_times.txt', 'w')#10以外重新寫入  schedule_times = 1 print 'write schedule_times start!' try:  fp.write(str(schedule_times)+'/n') finally:  fp.close()  print 'write schedule_times finish!'if __name__ == '__main__': schedule_times = ReadTimes() #if schedule_times > 10: # schedule_times = 0 print schedule_times schedule_times = schedule_times + 1 WriteTimes(schedule_times)

2.1、不加鎖對文件進行多線程讀寫。

file_lock.py

#! /usr/bin/env python#coding=utf-8from threading import Threadimport threadingimport timefrom ltz_schedule_times import *#1、不加鎖def lock_test(): time.sleep(0.1)  schedule_times = ReadTimes() print schedule_times schedule_times = schedule_times + 1 WriteTimes(schedule_times)if __name__ == '__main__': for i in range(5):  Thread(target = lock_test, args=()).start()

得到結果:

0write schedule_times start!write schedule_times finish!tmp: tmp: tmp: tmp:  [[[['1''1''1''1']]]]111 1write schedule_times start!write schedule_times start!write schedule_times start!write schedule_times start!write schedule_times finish!write schedule_times finish!write schedule_times finish!write schedule_times finish!

文件寫入結果:

Python多線程同步---文件讀寫控制

以上結果可以看出,不加鎖多線程讀寫文件會出現錯誤。

2.2、加鎖對文件進行多線程讀寫。

file_lock.py

#! /usr/bin/env python#coding=utf-8from threading import Threadimport threadingimport timefrom ltz_schedule_times import *#2、加鎖mu = threading.Lock() #1、創建一個鎖def lock_test(): #time.sleep(0.1)  if mu.acquire(True): #2、獲取鎖狀態,一個線程有鎖時,別的線程只能在外面等著  schedule_times = ReadTimes()  print schedule_times  schedule_times = schedule_times + 1  WriteTimes(schedule_times)  mu.release() #3、釋放鎖  if __name__ == '__main__': for i in range(5):  Thread(target = lock_test, args=()).start()

結果:

0write schedule_times start!write schedule_times finish!tmp: ['1']1write schedule_times start!write schedule_times finish!tmp: ['1', '2']2write schedule_times start!write schedule_times finish!tmp: ['1', '2', '3']3write schedule_times start!write schedule_times finish!tmp: ['1', '2', '3', '4']4write schedule_times start!write schedule_times finish!            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涿州市| 裕民县| 法库县| 大新县| 楚雄市| 公主岭市| 北安市| 周宁县| 青铜峡市| 彭水| 克山县| 阿拉尔市| 钦州市| 淮阳县| 定陶县| 宜阳县| 龙胜| 青浦区| 英德市| 大安市| 久治县| 金华市| 广水市| 沙雅县| 五寨县| 铜梁县| 西乌珠穆沁旗| 安塞县| 米林县| 斗六市| 孟连| 永川市| 陆川县| 武乡县| 临清市| 延安市| 措美县| 专栏| 根河市| 洛阳市| 寿光市|