欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年02月12日 09:05:46   作者:愛橙子的OK繃  
今天小編就為大家分享一篇Python多線程同步---文件讀寫控制方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

1、實(shí)現(xiàn)文件讀寫的文件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以內(nèi)追加進(jìn)去
 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、不加鎖對文件進(jìn)行多線程讀寫。

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!

文件寫入結(jié)果:

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

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

2.2、加鎖對文件進(jìn)行多線程讀寫。

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!

文件寫入結(jié)果:

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

以上這篇Python多線程同步---文件讀寫控制方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python小程序?qū)崿F(xiàn)刷票功能詳解

    python小程序?qū)崿F(xiàn)刷票功能詳解

    這篇文章主要介紹了python小程序?qū)崿F(xiàn)刷票功能詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python數(shù)據(jù)XPath使用案例詳解

    python數(shù)據(jù)XPath使用案例詳解

    xpath是最常用且最便捷高效的一種解析方式,通用型強(qiáng),其不僅可以用于python語言中,還可以用于其他語言中,數(shù)據(jù)解析建議首先xpath
    2021-09-09
  • Python的print函數(shù)如何覆蓋打印

    Python的print函數(shù)如何覆蓋打印

    這篇文章主要介紹了Python的print函數(shù)如何覆蓋打印問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python同步遍歷多個(gè)列表的示例

    Python同步遍歷多個(gè)列表的示例

    今天小編就為大家分享一篇Python同步遍歷多個(gè)列表的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • 淺談Python 集合(set)類型的操作——并交差

    淺談Python 集合(set)類型的操作——并交差

    下面小編就為大家?guī)硪黄獪\談Python 集合(set)類型的操作——并交差。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • Python如何爬取qq音樂歌詞到本地

    Python如何爬取qq音樂歌詞到本地

    這篇文章主要介紹了Python如何爬取qq音樂歌詞到本地,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • flask框架路由常用定義方式總結(jié)

    flask框架路由常用定義方式總結(jié)

    這篇文章主要介紹了flask框架路由常用定義方式,結(jié)合實(shí)例形式總結(jié)分析了flask框架路由的常見定義方式與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-07-07
  • Python3 實(shí)現(xiàn)串口兩進(jìn)程同時(shí)讀寫

    Python3 實(shí)現(xiàn)串口兩進(jìn)程同時(shí)讀寫

    今天小編就為大家分享一篇Python3 實(shí)現(xiàn)串口兩進(jìn)程同時(shí)讀寫,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python全棧之正則表達(dá)式

    Python全棧之正則表達(dá)式

    這篇文章主要為大家介紹了Python正則表達(dá)式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • Pytorch訓(xùn)練模型得到輸出后計(jì)算F1-Score 和AUC的操作

    Pytorch訓(xùn)練模型得到輸出后計(jì)算F1-Score 和AUC的操作

    這篇文章主要介紹了Pytorch訓(xùn)練模型得到輸出后計(jì)算F1-Score 和AUC的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評論