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实现的二叉树算法和kmp算法实例
Apr 25 Python
Python的MongoDB模块PyMongo操作方法集锦
Jan 05 Python
Python正则表达式使用经典实例
Jun 21 Python
python中json格式数据输出的简单实现方法
Oct 31 Python
深入理解NumPy简明教程---数组1
Dec 17 Python
python tornado使用流生成图片的例子
Nov 18 Python
Python调用.NET库的方法步骤
Dec 27 Python
keras的backend 设置 tensorflow,theano操作
Jun 30 Python
教你怎么用Python生成九宫格照片
May 20 Python
健身房被搭讪?用python写了个小米计时器助人为乐
Jun 08 Python
python使用PySimpleGUI设置进度条及控件使用
Jun 10 Python
PyCharm 配置SSH和SFTP连接远程服务器
May 11 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
异世界新番又来了,同样是从零开始,男主的年龄降到5岁
2020/04/09 日漫
PHP+MySQL插入操作实例
2015/01/21 PHP
PHP类的封装与继承详解
2015/09/29 PHP
在textarea文本域中显示HTML代码的方法
2007/03/06 Javascript
经常用的图片在容器中的水平垂直居中实例
2007/06/10 Javascript
javascript cookie解码函数(兼容ff)
2008/03/17 Javascript
js如何获取file控件的完整路径具体实现代码
2013/05/15 Javascript
点击A元素触发B元素的事件在IE8下会识别成A元素
2014/09/04 Javascript
js实现jquery的offset()方法实例
2015/01/10 Javascript
javascript单例模式的简单实现方法
2015/07/25 Javascript
jQuery+CSS3文字跑马灯特效的简单实现
2016/06/25 Javascript
vue.js指令v-for使用及索引获取
2016/11/03 Javascript
ajax异步请求详解
2017/01/06 Javascript
jQuery实现移动端Tab选项卡效果
2017/03/15 Javascript
详解Vue路由钩子及应用场景(小结)
2017/11/07 Javascript
Vue-Quill-Editor富文本编辑器的使用教程
2018/09/21 Javascript
vue请求数据的三种方式
2020/03/04 Javascript
vue使用svg文件补充-svg放大缩小操作(使用d3.js)
2020/09/22 Javascript
微信小程序自定义tabBar的踩坑实践记录
2020/11/06 Javascript
vue实现验证用户名是否可用
2021/01/20 Vue.js
[43:51]2014 DOTA2国际邀请赛中国区预选赛 Dream Times VS TongFu
2014/05/22 DOTA
轻松理解Python 中的 descriptor
2017/09/15 Python
Python数据可视化正态分布简单分析及实现代码
2017/12/04 Python
Python中跳台阶、变态跳台阶与矩形覆盖问题的解决方法
2018/05/19 Python
深入理解Python中的 __new__ 和 __init__及区别介绍
2018/09/17 Python
关于不懂Chromedriver如何配置环境变量问题解决方法
2019/06/12 Python
使用PyQt5实现图片查看器的示例代码
2020/04/21 Python
酒店管理专业学生求职信
2013/09/27 职场文书
少年闰土教学反思
2014/02/22 职场文书
省级青年文明号申报材料
2014/05/23 职场文书
2015圣诞节贺卡寄语
2015/03/24 职场文书
新员工试用期工作总结2015
2015/05/28 职场文书
预备党员半年考察意见
2015/06/01 职场文书
省级三好学生主要事迹材料
2015/11/03 职场文书
Java并发编程之详解CyclicBarrier线程同步
2021/06/23 Java/Android
微信小程序实现轮播图指示器
2022/06/25 Javascript