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 相关文章推荐
在IIS服务器上以CGI方式运行Python脚本的教程
Apr 25 Python
python实现ping的方法
Jul 06 Python
安装Python和pygame及相应的环境变量配置(图文教程)
Jun 04 Python
Python实现树的先序、中序、后序排序算法示例
Jun 23 Python
python进程管理工具supervisor的安装与使用教程
Sep 05 Python
Python给图像添加噪声具体操作
Mar 03 Python
解决python 3 urllib 没有 urlencode 属性的问题
Aug 22 Python
Python中filter与lambda的结合使用详解
Dec 24 Python
Python MySQL 日期时间格式化作为参数的操作
Mar 02 Python
python Django 反向访问器的外键冲突解决
May 20 Python
Python 详解通过Scrapy框架实现爬取CSDN全站热榜标题热词流程
Nov 11 Python
Django框架中视图的用法
Jun 10 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中的串行化变量和序列化对象
2006/09/05 PHP
PHP的栏目导航程序
2006/10/09 PHP
PHP取整数函数常用的四种方法小结
2012/07/05 PHP
基于PHP编程注意事项的小结
2013/04/27 PHP
PHP人民币金额转大写实例代码
2015/10/02 PHP
PHP中危险的file_put_contents函数详解
2017/11/04 PHP
php array 转json及java 转换 json数据格式操作示例
2019/11/13 PHP
jquery.alert 弹出式复选框实现代码
2009/06/15 Javascript
javawscript 三级菜单的实现原理
2009/07/01 Javascript
jQuery 打造动态渐变按钮 详细图文教程
2010/04/25 Javascript
Jquery跨域获得Json时invalid label错误的解决办法
2011/01/11 Javascript
jQuery 事件的命名空间简单了解
2013/11/22 Javascript
javascript定义变量时加var与不加var的区别
2014/12/22 Javascript
js+CSS实现模拟华丽的select控件下拉菜单效果
2015/09/01 Javascript
JS实现的在线调色板实例(附demo源码下载)
2016/03/01 Javascript
jQuery新窗口打开外链接
2016/07/21 Javascript
jquery遍历标签中自定义的属性方法
2016/09/17 Javascript
JS中用三种方式实现导航菜单中的二级下拉菜单
2016/10/31 Javascript
实现隔行换色效果的两种方式【实用】
2016/11/27 Javascript
vue服务端渲染的实例代码
2017/08/28 Javascript
ES6 javascript中class静态方法、属性与实例属性用法示例
2017/10/30 Javascript
微信小程序实现自上而下字幕滚动
2018/07/14 Javascript
探秘vue-rx 2.0(推荐)
2018/09/21 Javascript
微信小程序实现时间进度条功能
2020/11/17 Javascript
JavaScript中的函数申明、函数表达式、箭头函数
2019/12/06 Javascript
vue.js使用v-model实现父子组件间的双向通信示例
2020/02/05 Javascript
Vue3 的响应式和以前有什么区别,Proxy 无敌?
2020/05/20 Javascript
python实现批量按比例缩放图片效果
2018/03/30 Python
python监控文件并且发送告警邮件
2018/06/21 Python
修改python plot折线图的坐标轴刻度方法
2018/12/13 Python
Python编程中类与类的关系详解
2019/08/08 Python
Django实现WebSSH操作物理机或虚拟机的方法
2019/11/06 Python
基于python实现百度语音识别和图灵对话
2020/11/02 Python
String是最基本的数据类型吗?
2013/06/13 面试题
解决SpringBoot跨域的三种方式
2021/06/26 Java/Android
vue自定义右键菜单之全局实现
2022/04/09 Vue.js