python多线程同步之文件读写控制

 更新时间:2021年02月25日 13:18:35   作者:爱橙子的OK绷  
这篇文章主要为大家详细介绍了python多线程同步之文件读写控制,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python多线程同步之文件读写控制的具体代码,供大家参考,具体内容如下

1、实现文件读写的文件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以内追加进去
 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-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()

得到结果:

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!

文件写入结果:

以上结果可以看出,不加锁多线程读写文件会出现错误。

2.2、加锁对文件进行多线程读写。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、创建一个锁
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()

结果:

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!

文件写入结果:

达到读写效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python数据类型_元组、字典常用操作方法(介绍)

    python数据类型_元组、字典常用操作方法(介绍)

    下面小编就为大家带来一篇python数据类型_元组、字典常用操作方法(介绍)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 详解Python的Lambda函数与排序

    详解Python的Lambda函数与排序

    本篇文章主要是介绍了Python的Lambda函数与排序,简单的介绍了Lambda函数的用法和排序,有需要的朋友可以了解一下。
    2016-10-10
  • Python3.9新特性详解

    Python3.9新特性详解

    这篇文章主要介绍了Python3.9新特性详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Python 用__new__方法实现单例的操作

    Python 用__new__方法实现单例的操作

    这篇文章主要介绍了Python 用__new__方法实现单例的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 教你一步步利用python实现贪吃蛇游戏

    教你一步步利用python实现贪吃蛇游戏

    这篇文章主要给大家介绍了关于如何利用python实现贪吃蛇游戏的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • pytorch中的numel函数用法说明

    pytorch中的numel函数用法说明

    这篇文章主要介绍了pytorch中的numel函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-05-05
  • 基于Python实现一个专业的密码强度检测器

    基于Python实现一个专业的密码强度检测器

    这篇文章主要为大家详细介绍了基于Python实现一个专业的密码强度检测器,可以用于评估密码的安全性和强度,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-12-12
  • Python中DataFrame转列表的最全指南

    Python中DataFrame转列表的最全指南

    在Python数据分析中,Pandas的DataFrame是最常用的数据结构之一,本文将为你详解5种主流DataFrame转换为列表的方法,大家可以根据需求进行选择
    2025-03-03
  • Python3基础之函数用法

    Python3基础之函数用法

    这篇文章主要介绍了Python3的函数用法,非常重要,需要的朋友可以参考下
    2014-08-08
  • Python办公自动化之数据预处理和数据校验详解

    Python办公自动化之数据预处理和数据校验详解

    这篇文章主要为大家详细介绍了Python办公自动化中数据预处理和数据校验的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下
    2024-01-01

最新评论