Question
(1) Contents of file1.txt are: 34 56 87 12 98 113 771 88 641 13 (2) Contents of file2.txt are (double of the values of
(1) Contents of file1.txt are:
34 56 87 12 98 113 771 88 641 13
(2) Contents of file2.txt are (double of the values of file1.txt):
68 112 174 24 196 226 1542 176 1282 26
(3) split the files file1.txt and file2.txt into half (5 lines in each chunk) using the split command:
split -l5 file1.txt file1_ split -l5 file2.txt file2_
(4) Then I have below Python code which reads pieces file1_aa and file2_aa and some the values from them. Then file1_ab and file2_ab and does the same.
import threading
def process_chunk(file1, file2, start, end, output): with open(file1, "r") as f1, open(file2, "r") as f2, open(output, "a") as f3: for i in range(start, end): line1 = f1.readline().strip() line2 = f2.readline().strip() result = int(line1) + int(line2) f3.write(str(result) + ' ')
def main(): chunk_size = 2 file1 = "file1.txt" file2 = "file2.txt" output = "file3.txt" with open(file1, "r") as f: lines = f.readlines() num_chunks = len(lines) // chunk_size + (len(lines) % chunk_size != 0) start = 0 end = chunk_size threads = [] for i in range(num_chunks): t = threading.Thread(target=process_chunk, args=(file1, file2, start, end, output)) threads.append(t) t.start() start = end end += chunk_size if end > len(lines): end = len(lines) for t in threads: t.join()
if __name__ == "__main__": main()
(Task) Need to rewrite the above Python code so that parallelism can be seen means all corresponding chunks file1_aa/file2_aa and file1_ab/file2_ab are processed in parallel.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started