Py学习  »  Python

多进程python大文件读取

Dima Mintsman • 7 年前 • 2318 次点击  

我有一个二进制文件,其中包含固定数量的图像(每个图像的大小为1024*768)。我将每个图像放入joinablequeue,并在多处理中对其进行分析,它在处理小文件时工作得很好,但是当我试图读取大文件时会出现内存错误。 有人知道我如何存储大文件到Buff/Queas(作为字符串)?(不幸的是我不能使用经理或游泳池)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44037
文章 [ 2 ]  |  最新文章 7 年前
Rouzip
Reply   •   1 楼
Rouzip    7 年前
  1. 你可以阅读缓冲区 here .
  2. 如果你的记忆力很小,你可以试着强迫GC:
import gc

SIZE = 1024*768  
MEMOSIZE = 1024  # your memory size
with open('xxx', 'rb') as fp:  # open the file
    i = 0  # remember the number to gc in time
    queue = []
    while True:
        if (i*(SIZE-1) < MEMOSIZE):
            x = fp.read(SIZE)  # if your image is single channel
            queue.append(x)
            # do something
        else:
            del queue
            gc.collect()
CLpragmatics
Reply   •   2 楼
CLpragmatics    7 年前

你看了模块io.bytesio吗? 你可以在这里找到它: https://docs.python.org/release/3.1.3/library/io.html#binary-i-o 您可以设置缓冲区大小,这解决了我的内存问题。