python多线程读取文件的问题_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!
文件写入结果:
以上结果可以看出,不加锁多线程读写文件会出现错误。
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多线程同步---文件读写控制方法
本文地址: http://www.cppcns.com/jiaoben/python/251869.html
python多线程读取文件的问题_Python多线程同步---文件读写控制方法相关推荐
- 32. Pandas借助Python爬虫读取HTML网页表格存储到Excel文件
Pandas借助Python爬虫读取HTML网页表格存储到Excel文件 实现目标: 网易有道词典可以用于英语单词查询,可以将查询的单词加入到单词本; 当前没有导出全部单词列表的功能.为了复习方便,可 ...
- GIT项目管理工具(part4)--提交文件暂存记录及同步文件至本地仓库
本系列BLOG为鄙人学习GIT时的学习笔记.前一个知识点引出后一个知识点,后一个例子接着前一个例子. 文章目录 基本操作 3.将工作内容记录到暂存区 4.取消文件暂存记录 5.将文件同步到本地仓库 举 ...
- python批量读取excel表格数据_Python读取Excel数据并生成图表过程解析
一.需求背景 自己一直在做一个周基金定投模拟,每周需要添加一行数据,并生成图表.以前一直是用Excel实现的.但数据行多后,图表大小调整总是不太方便,一般只能通过缩放比例解决. 二.需求实现目标 通过 ...
- python xlrd读取excel所有数据_python读取excel进行遍历/xlrd模块操作
我就废话不多说了,大家还是直接看代码吧~ #!/usr/bin/env python # -*- coding: utf-8 -*- import csv import xlrd import xlw ...
- mfc读取txt文件并显示_Python入门丨文件读写
文件读写 文件读写,是Python代码调用调用电脑文件的主要功能,能被用于读取和写入文本记录.音频片段.Excel文档.保存邮件以及任何保存在电脑上的东西. 读取文件 读取文件三个步骤: 准备工作:首 ...
- python 批量读取xlsx并合并_python合并多个excel表格数据-python如何读取多个excel合并到一个excel中...
python如何读取多个excel合并到一个excel中 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文 ...
- python输出读取的空格数目_Python 3基础教程: 输入和输出具体代码实例
在前面文章中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数.( ...
- python 怎么将数组转为列表_Python怎么将文件读入列表?
读取日志文件的Python示例,一行一行地进入列表. # With '\n', ['1\n', '2\n', '3'] with open('/www/logs/server.log') as f: ...
- python中的文件怎么处理_python 中有关文件处理
Python的文件处理 打开文件f = open ("path","mode") r 模式 以读的方式打开,定位到文件开头 , 默认的 mode.文件不存在直接 ...
最新文章
- Go 分布式学习利器(18)-- Go并发编程之lock+WaitGroup实现线程安全
- python:面向对象初级
- 【 FPGA 】BUFHCE 案例浅析
- yield( )函数的使用
- 函数参数 不定参数,和 默认参数
- 1.8 Arrays工具类
- ITK:Mersenne Twister随机整数生成器
- 区块链跟银行有什么关系?
- 计算机网络课程设计之TELNET 终端设计与实现
- html5网页设计教程电子书,HTML5教程(使用HTML5设计网络富客户端应用)
- Spark集群试运行
- LINUX SHELL中for用数组循环
- org.xml.sax.SAXParseException: 缺少文件根组件。
- linux-文件系统格式区别xfs、ext4、ext3、ext2、vfat、swap、EFI system partition
- MASM5及LINK命令行
- 彻底禁止电脑弹出广告的方法(以2345好压为例)
- android证书在线生成方法
- 阿里巴巴实习一年之后的感悟
- 计算机表格标题怎么做,做表必备!超实用的五个制作Excel表头的技巧,快速学起来...
- 微信小程序+SpringBoot实现校园快递代收平台
热门文章
- 北京搜狗已签算法30W,西安银行总包20W,要不要毁约去银行?
- 用意念就能控制的操作系统?这家华人团队做的脑机接口,3个电极实现脑波成像...
- 如何看待研究生导师直接说你写的论文就是垃圾?
- 这里有个自学数据分析并拿到大厂offer的文科小姐姐
- P8可以年入170万,那P10级别的程序猿,每天都在干嘛?
- 全网最火的Nacos监控中心——Prometheus+Grafana
- 新视界,你好!_只愿与一人十指紧扣_新浪博客
- 数据可视化:Matplotlib vs ggplot2
- 机器视觉关键技术之图像增强技术
- 我收藏的谷歌和阿里大佬的刷题笔记