python多线程多进程运行场景(Python多线程同步---文件读写控制方法)
类别:脚本大全 浏览量:1769
时间:2022-03-30 00:48:03 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多线程同步---文件读写控制方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/will130/article/details/50599577
您可能感兴趣
- python多线程和多进程应用场景(Python多线程处理实例详解单进程/多进程)
- python多线程超时设置(解决python线程卡死的问题)
- python中的多线程详解(python多线程抽象编程模型详解)
- php脚本控制方法(php swoole多进程/多线程用法示例基于php7nts版)
- python 多线程与多进程(python 多线程串行和并行的实例)
- python多进程与多线程详解(Python线程之定位与销毁的实现)
- linux多线程怎么设置(超详细讲解Linux C++多线程同步的方式)
- python多线程实现(python多线程并发让两个LED同时亮的方法)
- python多线程并发使用场景(对python多线程SSH登录并发脚本详解)
- tomcat 多线程并发cpu(Tomcat使用线程池处理远程并发请求的方法)
- ftp上传工具使用方法(CuteFTP多线程FTP上传下载工具功能介绍)
- php怎么实现多线程(PHP实现的多进程控制demo示例)
- phpcurl请求能在日志里记录吗(php使用curl模拟多线程实现批处理功能示例)
- python的多线程比多进程效率高(Python中单线程、多线程和多进程的效率对比实验实例)
- python关闭程序强制退出线程(python多线程调用exit无法退出的解决方法)
- python多线程多种方法(详解python多线程之间的同步一)
- 新疆80后在淘宝卖干果 以前是 不务正业 如今帮乡亲致富(新疆80后在淘宝卖干果)
- 弄清楚了销 售 买 卖这四个字,母婴生意做起来就没那么难了(弄清楚了销售买)
- 数读 买首饰金是 投资黄金 吗 买金容易卖金难(数读买首饰金是)
- 销 售 买 卖 你真的了解这四个字了吗(销售买)
- 谢娜是得罪快乐大本营造型师了吗 全场被黑化(谢娜是得罪快乐大本营造型师了吗)
- 前《iLOOK》时装总监 《快乐大本营》御用造型师上线(快乐大本营御用造型师上线)
热门推荐
- python类的init方法(Python操作配置文件ini的三种方法讲解)
- apache域名绑定(Apache配置独立域名的方法)
- docker如何简化部署(Docker使用Portainer搭建可视化界面的方法)
- html中table怎么用(详解CSS的table-layout属性的用法)
- dedecms更新后设置空白(dedecms如何去掉首页index.html的方法)
- dockerip访问限制(docker win ping 不通容器 避坑指南)
- python能操作微信定时发消息(python给微信好友定时推送消息的示例)
- php 实例代码解析(PHP反射学习入门示例)
- mysql一般使用的事务隔离级别(详解MySQL中事务隔离级别的实现原理)
- sql中的exists详解(SQL中EXPLAIN命令的使用方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9