This article describes how to read large files by multiple processes in multiple parts in Python, and related skills related to Python multi-process operations and file multipart reading, for more information about how to read ultra-large files in multiple Python processes, see the example in this article. We will share this with you for your reference. The details are as follows:
Read ultra-large text files and use multi-process multipart reading to output each file separately
#-*-Coding: GBK-*-import urlparseimport datetimeimport osfrom multiprocessing import Process, Queue, Array, RLock "multi-process multipart file Reading" WORKERS = 4 BLOCKSIZE = too many file_size = 0def getFilesize (file ): "Get the size of the file to be read" global FILE_SIZE fstream = open (file, 'R') fstream. seek (0, OS. SEEK_END) FILE_SIZE = fstream. tell () fstream. close () def process_found (pid, array, file, rlock): global FILE_SIZE global JOB global PREFIX "process processing Args: pid: Process number array: inter-process shared queue, used to mark the end position of the file block read by each process. file: name of the read file. Each process first obtains the endpossition (startpossition + BLOCKSIZE) position where the current maximum value is startpossition from the array) if (startpossition + BLOCKSIZE)