Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8129167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:11:40+00:00 2026-06-06T08:11:40+00:00

I have a single big text file in which I want to process each

  • 0

I have a single big text file in which I want to process each line ( do some operations ) and store them in a database. Since a single simple program is taking too long, I want it to be done via multiple processes or threads.
Each thread/process should read the DIFFERENT data(different lines) from that single file and do some operations on their piece of data(lines) and put them in the database so that in the end, I have whole of the data processed and my database is dumped with the data I need.

But I am not able to figure it out that how to approach this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:11:45+00:00Added an answer on June 6, 2026 at 8:11 am

    What you are looking for is a Producer/Consumer pattern

    Basic threading example

    Here is a basic example using the threading module (instead of multiprocessing)

    import threading
    import Queue
    import sys
    
    def do_work(in_queue, out_queue):
        while True:
            item = in_queue.get()
            # process
            result = item
            out_queue.put(result)
            in_queue.task_done()
    
    if __name__ == "__main__":
        work = Queue.Queue()
        results = Queue.Queue()
        total = 20
    
        # start for workers
        for i in xrange(4):
            t = threading.Thread(target=do_work, args=(work, results))
            t.daemon = True
            t.start()
    
        # produce data
        for i in xrange(total):
            work.put(i)
    
        work.join()
    
        # get the results
        for i in xrange(total):
            print results.get()
    
        sys.exit()
    

    You wouldn’t share the file object with the threads. You would produce work for them by supplying the queue with lines of data. Then each thread would pick up a line, process it, and then return it in the queue.

    There are some more advanced facilities built into the multiprocessing module to share data, like lists and special kind of Queue. There are trade-offs to using multiprocessing vs threads and it depends on whether your work is cpu bound or IO bound.

    Basic multiprocessing.Pool example

    Here is a really basic example of a multiprocessing Pool

    from multiprocessing import Pool
    
    def process_line(line):
        return "FOO: %s" % line
    
    if __name__ == "__main__":
        pool = Pool(4)
        with open('file.txt') as source_file:
            # chunk the work into batches of 4 lines at a time
            results = pool.map(process_line, source_file, 4)
    
        print results
    

    A Pool is a convenience object that manages its own processes. Since an open file can iterate over its lines, you can pass it to the pool.map(), which will loop over it and deliver lines to the worker function. Map blocks and returns the entire result when its done. Be aware that this is an overly simplified example, and that the pool.map() is going to read your entire file into memory all at once before dishing out work. If you expect to have large files, keep this in mind. There are more advanced ways to design a producer/consumer setup.

    Manual “pool” with limit and line re-sorting

    This is a manual example of the Pool.map, but instead of consuming an entire iterable in one go, you can set a queue size so that you are only feeding it piece by piece as fast as it can process. I also added the line numbers so that you can track them and refer to them if you want, later on.

    from multiprocessing import Process, Manager
    import time
    import itertools 
    
    def do_work(in_queue, out_list):
        while True:
            item = in_queue.get()
            line_no, line = item
    
            # exit signal 
            if line == None:
                return
    
            # fake work
            time.sleep(.5)
            result = (line_no, line)
    
            out_list.append(result)
    
    
    if __name__ == "__main__":
        num_workers = 4
    
        manager = Manager()
        results = manager.list()
        work = manager.Queue(num_workers)
    
        # start for workers    
        pool = []
        for i in xrange(num_workers):
            p = Process(target=do_work, args=(work, results))
            p.start()
            pool.append(p)
    
        # produce data
        with open("source.txt") as f:
            iters = itertools.chain(f, (None,)*num_workers)
            for num_and_line in enumerate(iters):
                work.put(num_and_line)
    
        for p in pool:
            p.join()
    
        # get the results
        # example:  [(1, "foo"), (10, "bar"), (0, "start")]
        print sorted(results)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big text file (size well above 1G) and I want to
I have a single XML file that I want to index using Lucene.NET. The
I have this text file with customer, account and transaction information in. Each item
I have a big load of documents, text-files, that I want to search for
I need to do an integrity check for a single big file. I have
I have a single xml file and every new thread of the program (BHO)
I have a single xml document (data.xml), which I display as HTML using an
I have a single image file in a folder in my Eclipse project that
I have to take a bunch of columnar text files and integrate them into
I'm using php to take xml files and convert them into single line tab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.