Python多线程同步---文件读写控制方法


Posted in Python onFebruary 12, 2019

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 相关文章推荐
Python中用memcached来减少数据库查询次数的教程
Apr 07 Python
Python实现统计代码行的方法分析
Jul 12 Python
Python实现字符串与数组相互转换功能示例
Sep 22 Python
python中使用xlrd读excel使用xlwt写excel的实例代码
Jan 31 Python
python顺序的读取文件夹下名称有序的文件方法
Jul 11 Python
python-opencv 将连续图片写成视频格式的方法
Jan 08 Python
python字符串查找函数的用法详解
Jul 08 Python
python列表插入append(), extend(), insert()用法详解
Sep 14 Python
python基于gevent实现并发下载器代码实例
Nov 01 Python
Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析
Dec 26 Python
什么是python类属性
Jun 10 Python
Python字典取键、值对的方法步骤
Sep 30 Python
Python 按字典dict的键排序,并取出相应的键值放于list中的实例
Feb 12 #Python
Python 互换字典的键值对实例
Feb 12 #Python
Python根据成绩分析系统浅析
Feb 11 #Python
Python实现的在特定目录下导入模块功能分析
Feb 11 #Python
Python正则表达式和re库知识点总结
Feb 11 #Python
Python实现的大数据分析操作系统日志功能示例
Feb 11 #Python
Python实现对特定列表进行从小到大排序操作示例
Feb 11 #Python
You might like
PHP 面向对象实现代码
2009/11/11 PHP
PHP句法规则详解 入门学习
2011/11/09 PHP
简单的php中文转拼音的实现代码
2014/02/11 PHP
php实现在限定区域里自动调整字体大小的类实例
2015/04/02 PHP
PHP使用NuSOAP调用Web服务的方法
2015/07/18 PHP
thinkPHP简单调用函数与类库的方法
2017/03/15 PHP
PHP数组实际占用内存大小原理解析
2020/12/11 PHP
两种不同的方法实现js对checkbox进行全选和反选
2014/05/13 Javascript
js中的hasOwnProperty和isPrototypeOf方法使用实例
2014/06/06 Javascript
Javascript基础教程之break和continue语句
2015/01/18 Javascript
JavaScript简单表格编辑功能实现方法
2015/04/16 Javascript
bootstrap-table组合表头的实现方法
2017/09/07 Javascript
详解vue 兼容IE报错解决方案
2018/12/29 Javascript
利用d3.js制作连线动画图与编辑器的方法实例
2019/09/05 Javascript
vue项目在webpack2实现移动端字体自适配功能
2020/06/02 Javascript
[55:03]LGD vs EG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
[49:02]KG vs Infamous 2019国际邀请赛淘汰赛 败者组BO1 8.20.mp4
2020/07/19 DOTA
用python代码做configure文件
2014/07/20 Python
Python中字典的基本知识初步介绍
2015/05/21 Python
Python中文竖排显示的方法
2015/07/28 Python
详解Python中的相对导入和绝对导入
2017/01/06 Python
python数据结构之链表的实例讲解
2017/07/25 Python
python 实现的发送邮件模板【普通邮件、带附件、带图片邮件】
2019/07/06 Python
python集合的创建、添加及删除操作示例
2019/10/08 Python
Tensorflow 使用pb文件保存(恢复)模型计算图和参数实例详解
2020/02/11 Python
使用Python绘制台风轨迹图的示例代码
2020/09/21 Python
改变 Python 中线程执行顺序的方法
2020/09/24 Python
python 6种方法实现单例模式
2020/12/15 Python
HTML5 canvas画图并保存成图片的jcanvas插件
2014/01/17 HTML / CSS
腾讯技术类校园招聘笔试试题
2014/05/06 面试题
求职信模版
2013/11/30 职场文书
模范家庭事迹材料
2014/02/10 职场文书
洗手间标语
2014/06/23 职场文书
机关党员三严三实心得体会
2014/10/13 职场文书
开展党的群众路线教育实践活动情况汇报
2014/11/05 职场文书
团组织推荐意见
2015/06/05 职场文书