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 MySQLdb模块连接操作mysql数据库实例

    Python MySQLdb模块连接操作mysql数据库实例

    这篇文章主要介绍了Python MySQLdb模块连接操作mysql数据库实例,本文直接给出操作mysql代码实例,包含创建表、插入数据、插入多条数据、查询数据等内容,需要的朋友可以参考下
    2015-04-04
  • Python代码实现列表分组计数

    Python代码实现列表分组计数

    这篇文章主要介绍了Python代码实现列表分组计数,利用Python代码实现了使用分组函数对列表进行分组,并计算每组的元素个数的功能,需要的朋友可以参考一下
    2021-11-11
  • pytorch实现加载保存查看checkpoint文件

    pytorch实现加载保存查看checkpoint文件

    这篇文章主要介绍了pytorch实现加载保存查看checkpoint文件方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 解决Python pandas df 写入excel 出现的问题

    解决Python pandas df 写入excel 出现的问题

    今天小编就为大家分享一篇解决Python pandas df 写入excel 出现的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • 如何基于OpenCV&Python实现霍夫变换圆形检测

    如何基于OpenCV&Python实现霍夫变换圆形检测

    最近开始学习opencv,想检测图片上的圆环,发现霍夫变换可以做这样的效果出来,于是尝试用霍夫变换做了下圆环检测,这篇文章主要给大家介绍了基于OpenCV&Python实现霍夫变换圆形检测的相关资料,需要的朋友可以参考下
    2021-08-08
  • Python如何利用Har文件进行遍历指定字典替换提交的数据详解

    Python如何利用Har文件进行遍历指定字典替换提交的数据详解

    这篇文章主要给大家介绍了关于Python如何利用Har文件进行遍历指定字典替换提交的数据的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Python库skimage绘制二值图像代码实例

    Python库skimage绘制二值图像代码实例

    这篇文章主要介绍了Python库skimage绘制二值图像代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Python存储读取HDF5文件代码解析

    Python存储读取HDF5文件代码解析

    这篇文章主要介绍了Python存储读取HDF5文件代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • 使用python实现回文数的四种方法小结

    使用python实现回文数的四种方法小结

    今天小编就为大家分享一篇使用python实现回文数的四种方法小结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • python中Lambda表达式详解

    python中Lambda表达式详解

    在本篇文章里小编给大家整理的是关于python中Lambda表达式的相关知识点内容,有需要的朋友们可以学习下。
    2019-11-11

最新评论