python(35):多线程读取文件
多线程读取文件:
# _*_coding:utf-8_*_
import time, threading, ConfigParser '''
Reader类,继承threading.Thread
@__init__方法初始化
@run方法实现了读文件的操作
'''
class Reader(threading.Thread):
def __init__(self, file_name, start_pos, end_pos):
super(Reader, self).__init__()
self.file_name = file_name
self.start_pos = start_pos
self.end_pos = end_pos def run(self):
fd = open(self.file_name, 'r')
'''
该if块主要判断分块后的文件块的首位置是不是行首,
是行首的话,不做处理
否则,将文件块的首位置定位到下一行的行首
'''
if self.start_pos != :
fd.seek(self.start_pos-)
if fd.read() != '\n':
line = fd.readline()
self.start_pos = fd.tell()
fd.seek(self.start_pos)
'''
对该文件块进行处理
'''
while (self.start_pos <= self.end_pos):
line = fd.readline()
'''
do somthing
'''
self.start_pos = fd.tell() '''
对文件进行分块,文件块的数量和线程数量一致
'''
class Partition(object):
def __init__(self, file_name, thread_num):
self.file_name = file_name
self.block_num = thread_num def part(self):
fd = open(self.file_name, 'r')
fd.seek(, )
pos_list = []
file_size = fd.tell()
block_size = file_size/self.block_num
start_pos =
for i in range(self.block_num):
if i == self.block_num-:
end_pos = file_size-
pos_list.append((start_pos, end_pos))
break
end_pos = start_pos+block_size-
if end_pos >= file_size:
end_pos = file_size-
if start_pos >= file_size:
break
pos_list.append((start_pos, end_pos))
start_pos = end_pos+
fd.close()
return pos_list if __name__ == '__main__':
'''
读取配置文件
'''
config = ConfigParser.ConfigParser()
config.readfp(open('conf.ini'))
#文件名
file_name = config.get('info', 'fileName')
#线程数量
thread_num = int(config.get('info', 'threadNum'))
#起始时间
start_time = time.clock()
p = Partition(file_name, thread_num)
t = []
pos = p.part()
#生成线程
for i in range(thread_num):
t.append(Reader(file_name, *pos[i]))
#开启线程
for i in range(thread_num):
t[i].start()
for i in range(thread_num):
t[i].join()
#结束时间
end_time = time.clock()
print "Cost time is %f" % (end_time - start_time)
python(35):多线程读取文件的更多相关文章
- python中逐行读取文件的最佳方式_Drupal_新浪博客
python中逐行读取文件的最佳方式_Drupal_新浪博客 python中逐行读取文件的最佳方式 (2010-08-18 15:59:28) 转载▼ 标签: python ...
- python linecache模块读取文件的方法
转自: python linecache模块读取文件 在Python中,有个好用的模块linecache,该模块允许从任何文件里得到任何的行,并且使用缓存进行优化,常见的情况是从单个文件读取多行. l ...
- Python按行读取文件、写文件
Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...
- python用二进制读取文件
python二进制读取文件 很多时候,都需要用到二进制读取文件.毕竟很多信息不是以文本的形式存储.例如:图片,音乐等等.这些东西都是有规则的二进制文件. 在python中,二进制读取采用rb的方式. ...
- python,<一>读取文件open()
在实际操作中,我们经常会读取文件,这个时候python为我们提供了一个open()的方法,供我们读取文件,通过help(open),我们可以获取open的方法 f.close()关闭读取 f.read ...
- Python之批量读取文件【面试必学】
python的os模块可以实现普遍的操作系统功能,并且和平台无关.以下为实现根目录下文件的批量读取. os.listdir(dirname)可以列出dirname下的目录和文件,依次读取相应的文件即可 ...
- Python按顺序读取文件夹中文件
参考资料: https://blog.csdn.net/qq_22227123/article/details/79903116 https://blog.csdn.net/merdy_xi/arti ...
- 第9.5节 Python的readlines读取文件内容及其参数hint使用分析
一. 语法 readlines(hint=-1) readlines函数用于从文件或流中一次性读取多行数据,返回数据存入一个列表中. 参数hint释义: 这个参数在readlines的官方文档说明是用 ...
- python linecache模块读取文件用法详解
linecache模块允许从任何文件里得到任何的行,并且使用缓存进行优化,常见的情况是从单个文件读取多行. linecache.getlines(filename) 从名为filename的文件中得到 ...
随机推荐
- QTableView中使用Delegate方式来实现对特定列的文本进行换行
问题:由于表格的一个列中,有个别文本过长,默认情况下,QTableView不支持对某列的文本换行,所以需要通过Delegate方式来实现这样的效果 头文件的Delegate #pragma once ...
- 【PHP】Ajax跨域解决方案 、jsonp、cors
参考文章: 1.https://blog.csdn.net/u014727260/article/details/72793459 (后台java,实际上差不多) 2. 如何解决ajax跨域传输 数据 ...
- 【mysql】关于ICP、MRR、BKA等特性
一.Index Condition Pushdown(ICP) Index Condition Pushdown (ICP)是mysql使用索引从表中检索行数据的一种优化方式,从mysql5.6开始支 ...
- tensorflow没有代码提示的问题
在tensorflow包下的__init__.py文件中定义了一个contrib变量表示tensorflow.contrib包下的内容,但是tensorflow.contrib这个包是懒加载的,也就是 ...
- Spring使用内存数据库二
在上篇<Spring 使用内存数据库>中我们使用Hibernate作为ORM的框架,直接调用Hibernate的API进行相关的持久化操作,但在实际项目中,可能会由于公司规定等原因要求统一 ...
- memcached全面剖析--4. memcached的分布式算法
我是Mixi的长野. 第2次.第3次由前坂介绍了memcached的内部情况.本次不再介绍memcached的内部结构,开始介绍memcached的分布式. memcached的分布式 正如第1次中介 ...
- Redis 启动警告错误解决[转]
Redis 启动警告错误解决 启动错误 1.WARNING overcommit_memory is set to 0! Background save may fail under low memo ...
- cmake3.8X64编译opencv3.2出现opencv_ffmpeg、opencv_ffmpeg_64、ippicv_windows_20151201.zip无法下载问题解决方案
cmake版本:cmake3.8.0 开发环境:Visual Studio 2017 x64 解决方法:1.在opencv安装目录下sources\3rdparty\ffmpeg\ffmpeg.cma ...
- <welcome-file>index.action</welcome-file>直接设置action,404和struts2中的解决方案
这几天的项目页面的访问全部改为.action访问,在修改首页时遇到了问题.将web.xml文件中<welcome-file>index.action</welcome-file> ...
- scala+hadoop+spark环境搭建
一.JDK环境构建 在指定用户的根目录下编辑.bashrc文件,添加如下部分: # .bashrc # Source global definitions if [ -f /etc/bashrc ]; ...