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中文字符串截取问题
Jun 15 Python
深入理解python try异常处理机制
Jun 01 Python
python线程、进程和协程详解
Jul 19 Python
用Python写一段用户登录的程序代码
Apr 22 Python
python定时关机小脚本
Jun 20 Python
python如何生成网页验证码
Jul 28 Python
python根据url地址下载小文件的实例
Dec 18 Python
python爬虫超时的处理的实例
Dec 19 Python
python 遍历列表提取下标和值的实例
Dec 25 Python
python-numpy-指数分布实例详解
Dec 07 Python
Pycharm及python安装详细教程(图解)
Jul 31 Python
详解Python中import机制
Sep 11 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 __autoload()方法真的影响性能吗?
2012/03/30 PHP
php 判断数组是几维数组
2013/03/20 PHP
兼容ie6浏览器的php下载文件代码分享
2014/07/14 PHP
如何用PHP来实现一个动态Web服务器
2015/07/29 PHP
yii2.0框架场景的简单使用示例
2020/01/25 PHP
jQuery Ajax 全局调用封装实例代码详解
2016/06/02 Javascript
JavaScript Ajax实现异步通信
2016/12/14 Javascript
jQuery插件Echarts实现的双轴图效果示例【附demo源码下载】
2017/03/04 Javascript
Node.js常用工具之util模块
2017/03/09 Javascript
详解Nodejs之静态资源处理
2017/06/05 NodeJs
详解vue-cli构建项目反向代理配置
2017/09/07 Javascript
vue使用监听实现全选反选功能
2018/07/06 Javascript
详解Axios 如何取消已发送的请求
2018/10/20 Javascript
Vue.js递归组件实现组织架构树和选人功能
2019/07/04 Javascript
vue 集成 vis-network 实现网络拓扑图的方法
2019/08/07 Javascript
微信小程序实现同时上传多张图片
2020/02/03 Javascript
[03:31]DOTA2英雄基础教程 大地之灵
2013/12/17 DOTA
[58:59]完美世界DOTA2联赛PWL S3 access vs CPG 第一场 12.13
2020/12/16 DOTA
python中mechanize库的简单使用示例
2014/01/10 Python
用Python编写简单的定时器的方法
2015/05/02 Python
Python3最长回文子串算法示例
2019/03/04 Python
Python二叉树的镜像转换实现方法示例
2019/03/06 Python
Python 取numpy数组的某几行某几列方法
2019/10/24 Python
python里反向传播算法详解
2020/11/22 Python
python爬虫搭配起Bilibili唧唧的流程分析
2020/12/01 Python
CSS3 开发工具收集
2010/04/17 HTML / CSS
护理实习自我鉴定
2013/12/14 职场文书
总经理助理岗位职责范本
2014/07/20 职场文书
2014年领导班子专项整治整改方案
2014/09/28 职场文书
公安交警个人对照检查材料思想汇报
2014/10/01 职场文书
民间借贷纠纷答辩状
2015/08/03 职场文书
爱国主义教育主题班会
2015/08/13 职场文书
幼儿园大班开学寄语(2016秋季)
2015/12/03 职场文书
MySQL 如何设计统计数据表
2021/06/15 MySQL
手残删除python之后的补救方法
2021/06/26 Python
Golang日志包的使用
2022/04/20 Golang