麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

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

2020-02-23 06:27:13
字體:
來源:轉載
供稿:網友

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)+'') 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!            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品中文字幕一区 | 久久精品一二三区 | 国产美女视频一区二区三区 | 久久久久久高清 | 中文字幕在线永久 | 最新中文字幕在线 | 国产精品色综合 | 色综合网在线观看 | 亚洲精品一区国产精品丝瓜 | 免费毛片电影 | 久久久久久中文字幕 | 特黄一区二区三区 | 曰韩一二三区 | 久久免费视屏 | 久久久久久久久国产 | 91av亚洲| av大全在线免费观看 | 亚洲va在线| 久久亚洲视频网 | 亚洲网站在线播放 | 免费日韩片 | 国产精品视频一区二区三区综合 | 91成人免费视频 | a视频在线免费观看 | 成人福利电影在线观看 | 97视频| 中文字幕在线播放第一页 | 久久久婷婷一区二区三区不卡 | 精品一区二区三区不卡 | 日本视频免费看 | 日本网站一区二区三区 | 国产在线精品一区二区三区不卡 | 精品视频 久久久 | 成人在线高清视频 | 中文字幕一区二区三区久久 | 欧美视频黄色 | 五月婷婷第四色 | 欧美日性| 久久精品男人 | 一级电影在线免费观看 | 狠狠ri |