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


Posted in Python onFebruary 25, 2021

本文实例为大家分享了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!

文件写入结果:

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

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

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中利用Pandas库处理大数据的简单介绍
Apr 07 Python
Python实现按照指定要求逆序输出一个数字的方法
Apr 19 Python
python实现批量视频分帧、保存视频帧
May 31 Python
在Python函数中输入任意数量参数的实例
Jul 16 Python
Python 函数用法简单示例【定义、参数、返回值、函数嵌套】
Sep 20 Python
利用4行Python代码监测每一行程序的运行时间和空间消耗
Apr 22 Python
Python爬虫爬取百度搜索内容代码实例
Jun 05 Python
解决Keras 自定义层时遇到版本的问题
Jun 16 Python
python计算auc的方法
Sep 09 Python
python爬虫搭配起Bilibili唧唧的流程分析
Dec 01 Python
解决python绘图使用subplots出现标题重叠的问题
Apr 30 Python
Python打包为exe详细教程
May 18 Python
python线程中的同步问题及解决方法
Aug 29 #Python
python实现H2O中的随机森林算法介绍及其项目实战
Aug 29 #Python
flask/django 动态查询表结构相同表名不同数据的Model实现方法
Aug 29 #Python
深入了解python中元类的相关知识
Aug 29 #Python
Django shell调试models输出的SQL语句方法
Aug 29 #Python
python实现文件的分割与合并
Aug 29 #Python
Python配置文件处理的方法教程
Aug 29 #Python
You might like
PHP3 safe_mode 失效漏洞
2006/10/09 PHP
php下检测字符串是否是utf8编码的代码
2008/06/28 PHP
PHP队列用法实例
2014/11/05 PHP
Windows2003下php5.4安装配置教程(Apache2.4)
2016/06/30 PHP
php str_replace替换指定次数的方法详解
2017/05/05 PHP
TP5框架使用QueryList采集框架爬小说操作示例
2020/03/26 PHP
利用谷歌地图API获取点与点的距离的js代码
2012/10/11 Javascript
JS比较两个时间大小的简单示例代码
2013/12/20 Javascript
node.js中的dns.getServers方法使用说明
2014/12/08 Javascript
node.js中的fs.chown方法使用说明
2014/12/16 Javascript
Position属性之relative用法
2015/12/14 Javascript
jquery UI Datepicker时间控件冲突问题解决
2016/12/16 Javascript
微信小程序网络请求wx.request详解及实例
2017/05/18 Javascript
Vue.js实现的表格增加删除demo示例
2018/05/22 Javascript
JavaScript 中 JSON.parse 函数 和 JSON.stringify 函数
2018/12/05 Javascript
利用Node.js如何实现文件循环覆写
2019/04/05 Javascript
jQuery实现消息弹出框效果
2019/12/10 jQuery
Javascript如何递归遍历本地文件夹
2020/08/06 Javascript
使用原生javascript开发计算器实例代码
2021/02/21 Javascript
Python编写的com组件发生R6034错误的原因与解决办法
2013/04/01 Python
Python 如何访问外围作用域中的变量
2016/09/11 Python
Python进程间通信之共享内存详解
2017/10/30 Python
将matplotlib绘图嵌入pyqt的方法示例
2020/01/08 Python
python 读取二进制 显示图片案例
2020/04/24 Python
python中os包的用法
2020/06/01 Python
html5定制表单_动力节点Java学院整理
2017/07/11 HTML / CSS
Nike加拿大官网:Nike.com (CA)
2019/04/09 全球购物
How TDD works
2012/09/30 面试题
工作疏忽检讨书
2014/01/25 职场文书
西安交大自主招生自荐信
2014/01/27 职场文书
班级标语大全
2014/06/21 职场文书
中学生关于梦想的演讲稿
2014/08/22 职场文书
个人公司授权委托书范本
2014/10/12 职场文书
物业工程部经理岗位职责
2015/04/09 职场文书
导游词之云南丽江古城
2019/09/17 职场文书
Python爬虫入门案例之回车桌面壁纸网美女图片采集
2021/10/16 Python