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

  • Home
  • SEARCH
  • 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 4343728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:47:40+00:00 2026-05-21T11:47:40+00:00

Suppose I have some Python code like the following: input = open(input.txt) x =

  • 0

Suppose I have some Python code like the following:

input = open("input.txt")
x = (process_line(line) for line in input)
y = (process_item(item) for item in x)
z = (generate_output_line(item) + "\n" for item in y)
output = open("output.txt", "w")
output.writelines(z)

This code reads each line from the input file, runs it through several functions, and writes the output to the output file. Now I know that the functions process_line, process_item, and generate_output_line will never interfere with each other, and let’s assume that the input and output files are on separate disks, so that reading and writing will not interfere with each other.

But Python probably doesn’t know any of this. My understanding is that Python will read one line, apply each function in turn, and write the result to the output, and then it will read the second line only after sending the first line to the output, so that the second line does not enter the pipeline until the first one has exited. Do I understand correctly how this program will flow? If this is how it works, is there any easy way to make it so that multiple lines can be in the pipeline at once, so that the program is reading, writing, and processing each step in parallel?

  • 1 1 Answer
  • 2 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-05-21T11:47:41+00:00Added an answer on May 21, 2026 at 11:47 am

    You can’t really parallelize reading from or writing to files; these will be your bottleneck, ultimately. Are you sure your bottleneck here is CPU, and not I/O?

    Since your processing contains no dependencies (according to you), it’s trivially simple to use Python’s multiprocessing.Pool class.

    There are a couple ways to write this, but the easier w.r.t. debugging is to find independent critical paths (slowest part of the code), which we will make run parallel. Let’s presume it’s process_item.

    …And that’s it, actually. Code:

    import multiprocessing.Pool
    
    p = multiprocessing.Pool() # use all available CPUs
    
    input = open("input.txt")
    x = (process_line(line) for line in input)
    y = p.imap(process_item, x)
    z = (generate_output_line(item) + "\n" for item in y)
    output = open("output.txt", "w")
    output.writelines(z)
    

    I haven’t tested it, but this is the basic idea. Pool’s imap method makes sure results are returned in the right order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have the following b b/__init__.py b/c b/c/__init__.py b/c/d b/c/d/__init__.py In some python
Suppose you have some complex JSON like: {A : valA, B : { C
Propose to consider the following problem. Suppose we have some composite object. Are there
I have some not understanding actions from gnu clisp Suppose, I have some code
Suppose I have a file containing some lines: line 1 ... line 2 ...
Suppose you have the following layout for a python package ./a ./a/__init__.py ./a/_b.py inside
Suppose I have a function that performs some task (this is in Python pseudocode):
Suppose I have this Python code: from itertools import count, tee original = count()
I have code (Python) that must perform some operations regarding distances between reflected segments
I'm having some problems with the super function in Python. Suppose I have these

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.