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 多线程读写文件_Python多线程同步---文件读写控制方法相关推荐
- python 多线程读写文件_python多线程同步之文件读写控制
本文实例为大家分享了python多线程同步之文件读写控制的具体代码,供大家参考,具体内容如下 1.实现文件读写的文件ltz_schedule_times.py #! /usr/bin/env pyth ...
- python多线程下载多个文件_python多线程下载文件 练习1
***今天在我BF的帮助下完成了小小的程序,也是我第一次写博客呢,谢谢他哦嘎嘎 1.首先,计算出下载文件的长度. conn = urllib.urlopen(url) headers = conn.i ...
- python多线程处理文件_python多线程分块读取文件
# _*_coding:utf-8_*_ import time, threading, ConfigParser ''' Reader类,继承threading.Thread @__init__方法 ...
- python多线程数据交互_python 多线程 通信
一篇文章搞定Python多进程(全) 公众号:pythonislover 前面写了三篇关于python多线程的文章,大概概况了多线程使用中的方法,文章链接如下: 一篇文章搞懂Python多线程简单实现 ...
- python爬取天天基金_python多线程+代理池爬取天天基金网、股票数据过程解析
简介 提到爬虫,大部分人都会想到使用Scrapy工具,但是仅仅停留在会使用的阶段.为了增加对爬虫机制的理解,我们可以手动实现多线程的爬虫过程,同时,引入IP代理池进行基本的反爬操作. 本次使用天天基金 ...
- python读取手机文件_python 读取 网络 文件
Python之pandas数据加载.存储 Python之pandas数据加载.存储 0. 输入与输出大致可分为三类: 0.1 读取文本文件和其他更好效的磁盘存储格式 2.2 使用数据库中的数据 0.3 ...
- python web框架 多线程和多进程_python 多线程和多进程
多线程与多进程 知识预览 一 进程与线程的概念 二 threading模块 三 multiprocessing模块 四 协程 五 IO模型 回到顶部 一 进程与线程的概念 1.1 进程 考虑一个场景: ...
- python实用程序育儿法_Python多线程 简明例子
Python多线程 简明例子 (2010-03-11 15:15:09) Python多线程 简明例子 综述 多线程是程序设计中的一个重要方面,尤其是在服务器Deamon程序方面.无论何种系统,线程调 ...
- python真正实现多线程的方法_python多线程几种方法实现
匿名用户 1级 2018-05-27 回答 Python进阶(二十六)-多线程实现同步的四种方式 临界资源即那些一次只能被一个线程访问的资源,典型例子就是打印机,它一次只能被一个程序用来执行打印功能, ...
最新文章
- python json.loads()中文问题-解决Python下json.loads()中文字符出错的问题
- SQL Server 表和索引存储结构
- 日本推出机器人代理相亲,相亲现场帮你自我介绍
- Dart基础学习03--方法的使用
- 前端学习(3318):异步处理thunk
- Linux 文件夹权限
- 01-二维数组中的查找
- [Bzoj]5343: [Ctsc2018]混合果汁
- Eureka Server启动源码分析
- 【LeetCode】剑指 Offer 10- II. 青蛙跳台阶问题
- java与数据库的数据交互,Java与数据库初步交互(后续需要进行优化)
- 【转】如何读一篇论文
- 关于安装PHP补装PDO与PDO_MYSQL操作
- 【源码分享】python开发的高通平台fastboot刷机工具
- linux错误命令数字,Linux 命令及踩坑
- 转:读“DataBase Sharding at Netlog”,看DataBase Scale Out
- Photoshop - CMYK 和 RGB 区别是什么?
- 词干提取(stemming)与词形还原(lemmatization)
- 非科班小硕的算法秋招记录
- 有人在远程使用计算机是什么意思,如何远程控制计算机,计算机远程控制有什么用途...
热门文章
- 数据可视化工具的意义有哪些
- python打印四种三角形
- linux 递归查看文件个数,Linux下递归读取文件数量
- 程序win10_只需3步!教你打造精简win10,去除系统自带程序,运行更快!
- aref无效 lisp_Common Lisp专题4:数组
- python教程吾爱破解_2020年最新python入门到精通教程
- 华为裸金属服务器 虚拟化性能下降,服务器虚拟化 配置
- AcWing 894. 拆分-Nim游戏
- 畅通工程(并查集模版题)
- mysql 执行计划 改变_数据量增加导致mysql执行计划改变解决_MySQL