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实现服务器中只重载被修改的进程的方法
Apr 30 Python
python列出目录下指定文件与子目录的方法
Jul 03 Python
python机器学习之神经网络(二)
Dec 20 Python
python中csv文件的若干读写方法小结
Jul 04 Python
pycham查看程序执行的时间方法
Nov 29 Python
Python循环实现n的全排列功能
Sep 16 Python
100行Python代码实现每天不同时间段定时给女友发消息
Sep 27 Python
Python+OpenCV实现实时眼动追踪的示例代码
Nov 11 Python
Python内置异常类型全面汇总
May 28 Python
Python的3种运行方式:命令行窗口、Python解释器、IDLE的实现
Oct 10 Python
python实现scrapy爬虫每天定时抓取数据的示例代码
Jan 27 Python
Python 如何实现文件自动去重
Jun 02 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&amp;mysql(三)
2006/10/09 PHP
php5.2.0内存管理改进
2007/01/22 PHP
如何提高MYSQL数据库的查询统计速度 select 索引应用
2007/04/11 PHP
php实现的仿阿里巴巴实现同类产品翻页
2009/12/11 PHP
php超快高效率统计大文件行数
2015/07/05 PHP
PHP从二维数组得到N层分类树的实现代码
2016/10/11 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
javascript firefox兼容ie的dom方法脚本
2008/05/18 Javascript
捕获关闭窗口的脚本
2009/01/10 Javascript
kmock javascript 单元测试代码
2011/02/06 Javascript
仿谷歌主页js动画效果实现代码
2013/07/14 Javascript
js获取html页面节点方法(递归方式)
2013/12/13 Javascript
基于jquery的手风琴图片展示效果实现方法
2014/12/16 Javascript
JavaScript中的对象的extensible属性介绍
2014/12/30 Javascript
javascript中Function类型详解
2015/04/28 Javascript
jQuery实现打开网页自动弹出遮罩层或点击弹出遮罩层功能示例
2017/10/19 jQuery
用vue 实现手机触屏滑动功能
2020/05/28 Javascript
如何在JavaScript中等分数组的实现
2020/12/13 Javascript
Python实现插入排序和选择排序的方法
2019/05/12 Python
python粘包问题及socket套接字编程详解
2019/06/29 Python
python应用文件读取与登录注册功能
2019/09/23 Python
Python数据存储之 h5py详解
2019/12/26 Python
浅析Python3 pip换源问题
2020/01/06 Python
PyQt5 closeEvent关闭事件退出提示框原理解析
2020/01/08 Python
CSS3制作酷炫的三维相册效果
2016/07/01 HTML / CSS
Sunglass Hut巴西网上商店:男女太阳镜
2020/10/04 全球购物
求职推荐信
2013/10/28 职场文书
外贸业务员的岗位职责
2013/11/23 职场文书
护士进修自我鉴定
2014/02/07 职场文书
镇创先争优活动总结
2014/08/28 职场文书
四风查摆问题及整改措施
2014/10/10 职场文书
英语复习计划
2015/01/19 职场文书
2015年妇联工作总结范文
2015/04/22 职场文书
jquery插件实现悬浮的菜单
2021/04/24 jQuery
SQL SERVER实现连接与合并查询
2022/02/24 SQL Server
十大最强妖精系宝可梦,哲尔尼亚斯实力最强,第五被称为大力士
2022/03/18 日漫