python对大文件的处理
多线程框架中采取queue来实现线程间资源的互斥。
在文件过大的情况下,如果都读入内存的话,占用内存就太多了。
这里手动实现了一个多线程调用文件迭代器来使用f.next()
# -*- coding:utf-8 -*-
import threading
class Geturl(object):
def __init__(self, open_file):
self.open_file = open_file
self.num = 0
self.__mutex = threading.RLock()
self.f = open(self.open_file, 'r')
self.kafka_mutex = threading.RLock()
def _line(self):
self.__mutex.acquire()
try:
line = self.f.__next__()
except StopIteration:
line = StopIteration
self.f.close()
'''
if self.num % 1000 == 0:
print(self.num)
self.num += 1
'''
self.__mutex.release()
return line
def get_line(self):
return self._line()
def _deal(deal_file):
while True:
try:
item = deal_file.get_line()
# 处理工作
if item == StopIteration:
raise ValueError()
except ValueError:
print("all task has done!")
break
except Exception as e:
print("error:", e)
if __name__ == "__main__":
filename = 'Bigfile'
geturl = Geturl(filename)
_deal(geturl)
python对大文件的处理的更多相关文章
- 如何使用Python读取大文件
背景 最近处理文本文档时(文件约2GB大小),出现memoryError错误和文件读取太慢的问题,后来找到了两种比较快Large File Reading 的方法,本文将介绍这两种读取方法. 准备工作 ...
- Python读取大文件(GB)
Python读取大文件(GB) - CSDN博客 https://blog.csdn.net/shudaqi2010/article/details/54017766
- 强悍的Python读取大文件的解决方案
这是一道著名的 Python 面试题,考察的问题是,Python 读取大文件和一般规模的文件时的区别,也即哪些接口不适合读取大文件. 1. read() 接口的问题 f =open(filename, ...
- PHP 与Python 读取大文件的区别
php读取大文件的方法 <?php function readFile($file) { # 打开文件 $handle = fopen($file, 'rb'); while (feof($ ...
- python下载大文件
1. wget def download_big_file_with_wget(url, target_file_name): """ 使用wget下载大文件 Note: ...
- Python读取大文件的"坑“与内存占用检测
python读写文件的api都很简单,一不留神就容易踩"坑".笔者记录一次踩坑历程,并且给了一些总结,希望到大家在使用python的过程之中,能够避免一些可能产生隐患的代码. 1. ...
- python读取大文件
最近在学习python的过程中接触到了python对文件的读取.python读取文件一般情况是利用open()函数以及read()函数来完成: f = open(filename,'r') f.rea ...
- 使用python读取大文件
python中读取数据的时候有几种方法,无非是read,readline,readlings和xreadlines几种方法,在几种方法中,read和xreadlines可以作为迭代器使用,从而在读取大 ...
- python读取大文件的方法及mmap内存映射模块
python计算文件的行数和读取某一行内容的实现方法 :最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的 ...
- Python——读取大文件(GB)
最近处理文本文档时(文件约2GB大小),出现memoryError错误和文件读取太慢的问题,后来找到了两种比较快Large File Reading 的方法,本文将介绍这两种读取方法. Prelimi ...
随机推荐
- MX2怎样利用Fiddler进行网络数据抓包
首先须要保证PC与手机在同一局域网内或有独立公网IP, 下面以在同一局域网为例(保证手机能訪问到这台PC机器): 1. PC端配置 1). 安装Fiddler 2). 开启Fiddler下面功能: ...
- oc24--description
// Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; double ...
- [POJ 3621] Sightseeing Cows
[题目链接] http://poj.org/problem?id=3621 [算法] 01分数规划(最优比率环) [代码] #include <algorithm> #include &l ...
- Eclipse 连接hsqldb数据库
初学Java,在接触数据库根本无从下手,不知如何将程序和数据库连接起来,今天做一个记录. 数据库是:hsqldb_1_8_0_5 附链接百度云盘 链接:https://pan.baidu.com/s/ ...
- SQLServer 行转列,统计,二次分组
create table test ( bizdate datetime, --日期 classes varchar(50), --班次 '白班' 或 '夜班' qty int --产量 ...
- ModelState对象
1.在控制器中判断Model验证结果
- javascript一个作用域案例分析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C#自定义控件实现控件随窗口大小改变
1.新建用户控件,取名MyForm. 2.将默认的UserControl改成Form 3.在类中添加以下代码 private float X, Y; //获得控件的长度.宽度.位置.字体大小的数据 p ...
- Caffe: Caffe的Python接口
官方参考:http://caffe.berkeleyvision.org/installation.html 官方介绍是这样的: Python The main requirements are nu ...
- 目录处理文件&链接命令
一.目录处理文件 1.删除文件或目录 rm -rf [文件或目录] //remove:删除文件或目录 -r:删除目录 -f:强制 2.复制文件或目录 cp [选项] [原文件或 ...