Python multi-process chunking method for reading large files

Source: Internet
Author: User
In this paper, we describe the method of Python multi-process block reading large files. Share to everyone for your reference, as follows:

Reads oversized text files, uses multi-process block reads, and outputs each piece separately as a file

#-*-CODING:GBK-*-import urlparseimport datetimeimport osfrom multiprocessing import Process,queue,array,rlock "" "  Multi-process chunked Read file "" "WORKERS = 4BLOCKSIZE = 100000000file_size = 0def getfilesize (file):" "Gets the size of the file to 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 J OB Global PREFIX "" "Process processing Args:pid: Process number array: Inter-process Shared Queue to mark the end of file blocks read by each process file: the names of the files read by each process first from the array Gets the current maximum value for the start position startpossition End position Endpossition (startpossition+blocksize) if (startpossition+blocksize)
 
  

More readers interested in Python related content can view the topic: Python string manipulation tips Summary, Python Introductory and Advanced classic tutorials, and Python file and directory Operations Tips Summary

I hope this article is helpful for Python program design.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.