python多线程同步之文件读写控制


Posted in Python onFebruary 25, 2021

本文实例为大家分享了python多线程同步之文件读写控制的具体代码,供大家参考,具体内容如下

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之正规地说一句话
Sep 28 Python
Python检测网站链接是否已存在
Apr 07 Python
浅析Python中return和finally共同挖的坑
Aug 18 Python
Python基于回溯法子集树模板解决0-1背包问题实例
Sep 02 Python
在CentOS6上安装Python2.7的解决方法
Jan 09 Python
Python实现的凯撒密码算法示例
Apr 12 Python
Python3.6简单反射操作示例
Jun 14 Python
详解如何设置Python环境变量?
May 13 Python
python将print输出的信息保留到日志文件中
Sep 27 Python
tornado+celery的简单使用详解
Dec 21 Python
Elasticsearch py客户端库安装及使用方法解析
Sep 14 Python
Python非单向递归函数如何返回全部结果
Dec 18 Python
python线程中的同步问题及解决方法
Aug 29 #Python
python实现H2O中的随机森林算法介绍及其项目实战
Aug 29 #Python
flask/django 动态查询表结构相同表名不同数据的Model实现方法
Aug 29 #Python
深入了解python中元类的相关知识
Aug 29 #Python
Django shell调试models输出的SQL语句方法
Aug 29 #Python
python实现文件的分割与合并
Aug 29 #Python
Python配置文件处理的方法教程
Aug 29 #Python
You might like
用libtemplate实现静态网页生成
2006/10/09 PHP
中英文字符串翻转函数
2008/12/09 PHP
PHP 在线翻译函数代码
2009/05/07 PHP
浅谈php中mysql与mysqli的区别分析
2013/06/10 PHP
php实现的zip文件内容比较类
2014/09/24 PHP
PHP安装threads多线程扩展基础教程
2015/11/17 PHP
PHP实现中国公民身份证号码有效性验证示例代码
2017/05/03 PHP
javascript入门·动态的时钟,显示完整的一些方法,新年倒计时
2007/10/01 Javascript
jQuery之自动完成组件的深入解析
2013/06/19 Javascript
Javascript中使用parseInt函数需要注意的问题
2015/04/02 Javascript
javascript十六进制及二进制转化的方法
2015/05/06 Javascript
原生javascript+css3编写的3D魔方动画旋扭特效
2016/03/14 Javascript
JS脚本实现动态给标签控件添加事件的方法
2016/06/02 Javascript
第七篇Bootstrap表单布局实例代码详解(三种表单布局)
2016/06/21 Javascript
jQuery的中 is(':visible') 解析及用法(必看)
2017/02/12 Javascript
jQuery Validate表单验证插件实现代码
2017/06/08 jQuery
VUE 实现滚动监听 导航栏置顶的方法
2018/09/11 Javascript
[54:43]DOTA2-DPC中国联赛 正赛 CDEC vs Dynasty BO3 第一场 2月22日
2021/03/11 DOTA
Python生成随机数组的方法小结
2017/04/15 Python
python实现日常记账本小程序
2018/03/10 Python
python tornado修改log输出方式
2019/11/18 Python
Python字典底层实现原理详解
2019/12/18 Python
德国街头和运动文化高品质商店:BSTN Store
2017/08/26 全球购物
Notino法国:购买香水和化妆品
2019/04/15 全球购物
实习护士自我鉴定
2013/10/13 职场文书
客户服务经理岗位职责
2014/01/29 职场文书
大三学生做职业规划:给未来找个方向
2014/02/24 职场文书
计算机维护专业推荐信
2014/02/27 职场文书
道德之星事迹材料
2014/05/03 职场文书
2014市国税局对照检查材料思想汇报
2014/09/23 职场文书
迎新晚会主持词开场白
2015/05/28 职场文书
工作年限证明范本
2015/06/15 职场文书
导游词之嵊泗列岛
2019/10/30 职场文书
sql server删除前1000行数据的方法实例
2021/08/30 SQL Server
Go 通过结构struct实现接口interface的问题
2021/10/05 Golang
疑《守望先锋2》A测截图泄露 或将推出新模式、新界面
2022/04/03 其他游戏