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 7819571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:00:17+00:00 2026-06-02T07:00:17+00:00

I have a number of large files with many thousands of lines in python

  • 0

I have a number of large files with many thousands of lines in python dict format. I’m converting them with json.dumps to json strings.

import json
import ast

mydict = open('input', 'r')
output = open('output.json', "a")

for line in mydict:
        line = ast.literal_eval(line)
        line = json.dumps(line)
        output.write(line)
        output.write("\n")

This works flawlessly, however, it does so in a single threaded fashion. Is there an easy way to utilize the remaining cores in my system to speed things up?

Edit:

Based on the suggestions I’ve started here with the multiprocessing library:

import os
import json
import ast
from multiprocessing import Process, Pool

mydict = open('twosec.in', 'r')

def info(title):
        print title
        print 'module name:', __name__
        print 'parent process: ', os.getppid()
        print 'process id:', os.getpid()

def converter(name):
        info('converter function')
        output = open('twosec.out', "a")
        for line in mydict:
                line = ast.literal_eval(line)
                line = json.dumps(line)
                output.write(line)
                output.write("\n")

if __name__ == '__main__':
        info('main line')
        p = Process(target=converter, args=(mydict))
        p.start()
        p.join()

I don’t quite understand where Pool comes into play, can you explain more?

  • 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-02T07:00:20+00:00Added an answer on June 2, 2026 at 7:00 am

    Wrap the code above in a function that takes as its single argument a filename and that writes the json to an output file.

    Then create a Pool object from the multiprocessing module, and use Pool.map() to apply your function in parallel to the list of all files. This will automagically use all cores on your CPU, and because it uses multiple processes instead of threads, you won’t run into the global interpreter lock.

    Edit: Change the main portion of your program like so;

      if __name__ == '__main__':
         files = ['first.in', 'second.in', 'third.in'] # et cetera
         info('main line')
         p = Pool()
         p.map(convertor, files)
         p.close()
    

    Of course you should also change convertor() to derive the output name from the input name!

    Below is a complete example of a program to convert DICOM files into PNG format, using the ImageMagick program

    "Convert DICOM files to PNG format, remove blank areas."
    
    import os
    import sys # voor argv.
    import subprocess
    from multiprocessing import Pool, Lock
    
    def checkfor(args):
        try:
            subprocess.check_output(args, stderr=subprocess.STDOUT)
        except CalledProcessError:
            print "Required program '{}' not found! exiting.".format(progname)
            sys.exit(1)
    
    def processfile(fname):
        size = '1574x2048'
        args = ['convert', fname, '-units', 'PixelsPerInch', '-density', '300', 
                '-crop', size+'+232+0', '-page', size+'+0+0', fname+'.png']
        rv = subprocess.call(args)
        globallock.acquire()
        if rv != 0:
            print "Error '{}' when processing file '{}'.".format(rv, fname)
        else:
            print "File '{}' processed.".format(fname)
        globallock.release()
    
    ## This is the main program ##
    if __name__ == '__main__':
        if len(sys.argv) == 1:
            path, binary = os.path.split(sys.argv[0])
            print "Usage: {} [file ...]".format(binary)
            sys.exit(0)
        checkfor('convert')
        globallock = Lock()
        p = Pool()
        p.map(processfile, sys.argv[1:])
        p.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of really large files with many insert statements (18.7 million
I have many large (~30 MB a piece) tab-delimited text files with variable-width lines.
I have a number of large data files that I included in projects attributed
I have a large number of csv files that look like this below: xxxxxxxx
Hi i have requirement to process large number of files via multithreading in java.
I wrote Python script that processes big number of large text files and may
I have a program that can accept a large number of valid arguments. Many
I have a large number of basic text, rtf, html, pdf and chm files
I've got a number of directories with a large number of files in them
I'm using Python to transfer (via scp) and database a large number of files.

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.