日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python多線程同步之文件讀寫(xiě)控制

系統(tǒng) 1687 0

本文實(shí)例為大家分享了python多線程同步之文件讀寫(xiě)控制的具體代碼,供大家參考,具體內(nèi)容如下

1、實(shí)現(xiàn)文件讀寫(xiě)的文件ltz_schedule_times.py

            
#! /usr/bin/env python
#coding=utf-8
import os

def 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_times

def WriteTimes(schedule_times):
  if schedule_times <= 10:
    fp = open('schedule_times.txt', 'a+')#10以?xún)?nèi)追加進(jìn)去
  else:
    fp = open('schedule_times.txt', 'w')#10以外重新寫(xiě)入
    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、不加鎖對(duì)文件進(jìn)行多線程讀寫(xiě)。file_lock.py

            
#! /usr/bin/env python
#coding=utf-8

from threading import Thread
import threading
import time
from 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()
          

得到結(jié)果:

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


11

1
 1
write 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!
          

文件寫(xiě)入結(jié)果:

python多線程同步之文件讀寫(xiě)控制_第1張圖片

以上結(jié)果可以看出,不加鎖多線程讀寫(xiě)文件會(huì)出現(xiàn)錯(cuò)誤。

2.2、加鎖對(duì)文件進(jìn)行多線程讀寫(xiě)。file_lock.py

            
#! /usr/bin/env python
#coding=utf-8

from threading import Thread
import threading
import time
from ltz_schedule_times import *

#2、加鎖
mu = threading.Lock() #1、創(chuàng)建一個(gè)鎖
def lock_test():
  #time.sleep(0.1) 
  if mu.acquire(True): #2、獲取鎖狀態(tài),一個(gè)線程有鎖時(shí),別的線程只能在外面等著
    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()
          

結(jié)果:

            
0
write schedule_times start!
write schedule_times finish!
tmp: ['1']
1
write schedule_times start!
write schedule_times finish!
tmp: ['1', '2']
2
write schedule_times start!
write schedule_times finish!
tmp: ['1', '2', '3']
3
write schedule_times start!
write schedule_times finish!
tmp: ['1', '2', '3', '4']
4
write schedule_times start!
write schedule_times finish!
          

文件寫(xiě)入結(jié)果:

python多線程同步之文件讀寫(xiě)控制_第2張圖片

達(dá)到讀寫(xiě)效果。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 思茅市| 乾安县| 临猗县| 鄂尔多斯市| 龙岩市| 志丹县| 房产| 定远县| 阿拉善盟| 长汀县| 嘉峪关市| 扶绥县| 高青县| 德惠市| 合作市| 崇仁县| 宁武县| 临朐县| 江达县| 新郑市| 宜兰市| 同江市| 仁布县| 土默特左旗| 宁德市| 平舆县| 武隆县| 博客| 长顺县| 新营市| 晋中市| 乌拉特后旗| 诸暨市| 凤城市| 都江堰市| 工布江达县| 营口市| 边坝县| 莒南县| 吉林省| 郁南县|