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实现网站文件的全备份和差异备份
Nov 30 Python
Python爬取国外天气预报网站的方法
Jul 10 Python
python实现计算倒数的方法
Jul 11 Python
Python实现基于多线程、多用户的FTP服务器与客户端功能完整实例
Aug 18 Python
Django数据库表反向生成实例解析
Feb 06 Python
快速解决PyCharm无法引用matplotlib的问题
May 24 Python
Python补齐字符串长度的实例
Nov 15 Python
Dlib+OpenCV深度学习人脸识别的方法示例
May 14 Python
Python numpy.zero() 初始化矩阵实例
Nov 27 Python
Django框架models使用group by详解
Mar 11 Python
Django QuerySet查询集原理及代码实例
Jun 13 Python
python3实现无权最短路径的方法
May 12 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
COM in PHP (winows only)
2006/10/09 PHP
PHP截断标题且兼容utf8和gb2312编码
2013/09/22 PHP
PHP实现使用优酷土豆视频地址获取swf播放器分享地址
2014/06/05 PHP
thinkphp常见路径用法分析
2014/12/02 PHP
PHP读取CSV大文件导入数据库的实例
2017/07/24 PHP
yii2安装详细流程
2018/05/23 PHP
php使用curl_init()和curl_multi_init()多线程的速度比较详解
2018/08/15 PHP
表单提交验证类
2006/07/14 Javascript
JavaScript中的new的使用方法与注意事项
2007/05/16 Javascript
js 新浪的一个图片播放图片轮换效果代码
2008/07/15 Javascript
JS来动态的修改url实现对url的增删查改
2014/09/05 Javascript
利用Bootstrap实现表格复选框checkbox全选
2016/12/21 Javascript
简单实现js无缝滚动效果
2017/02/05 Javascript
Vue的Class与Style绑定的方法
2017/09/01 Javascript
Vue 项目部署到服务器的问题解决方法
2017/12/05 Javascript
vue实现图片加载完成前的loading组件方法
2018/02/05 Javascript
微信小程序文字显示换行问题
2019/07/28 Javascript
JS动态显示倒计时效果
2019/12/12 Javascript
python实现二维码扫码自动登录淘宝
2016/12/27 Python
Python实现一个Git日志统计分析的小工具
2017/12/14 Python
Python实现批量压缩图片
2018/01/25 Python
解决python3中解压zip文件是文件名乱码的问题
2018/03/22 Python
python 实现语音聊天机器人的示例代码
2018/12/02 Python
利用Python对文件夹下图片数据进行批量改名的代码实例
2019/02/21 Python
Python实现京东秒杀功能代码
2019/05/16 Python
美国求婚钻戒网站:Super Jeweler
2016/08/27 全球购物
澳大利亚新奇小玩意网站:Yellow Octopus
2017/12/28 全球购物
美国球鞋寄卖网站:Stadium Goods
2018/05/09 全球购物
2019史上最全Database工程师题库
2015/12/06 面试题
大学生村官典型材料
2014/01/12 职场文书
根叔历年演讲稿
2014/05/20 职场文书
公司新人试用期自我评价
2014/09/17 职场文书
就业协议书盖章的注意事项
2014/09/28 职场文书
2014年计生协会工作总结
2014/11/21 职场文书
2015年电工工作总结
2015/04/10 职场文书
详解Nginx启动失败的几种错误处理
2021/04/01 Servers