-1

Hello Friends, I hope someone check my code and helping me on this issue.

I want to read from multiple text files (at least 4) sequentially and print their content on the screen

  • First time not using Threading
  • Measure the elapsed time in both cases multiple time and calculate the average

this my Code:

import pandas as pd
from datetime import datetime 

start_time = datetime.now() 

text1 = pd.read_csv('alice_in_wonderland.txt', delimiter = "\t")
print(text1)
text2 = pd.read_csv('On-Sunset-Highways-Thomas-D-Murph.txt', delimiter = "\t")
print(text2)
text3 = pd.read_csv('History-of-Texas-Lan-Bill-Allcorn.txt', delimiter = "\t")
print(text3)
text4 = pd.read_csv('A-Secret-of-the-Sea--T-W-Thomas.txt', delimiter = "\t")
print(text4)

time_elapsed = datetime.now() - start_time 

print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))

From Here I have an issue how I make a multithreading by python. I want to make a 4 threads to read from text files and print on the screen , Also I want to

  • Measure the elapsed time multiple times.
  • record the results.
  • calculate the average time. Note: number of files = 4 text files.
  • 2
    Use the [`timeit`](https://docs.python.org/3/library/timeit.html) module. It works much better than what you're doing now. – MattDMo Jan 01 '22 at 01:25
  • Thanks for helpful advice, I will update my code now to use 'timeit', How I can read multiple text files by multithreading – Badr Khaled Jan 01 '22 at 01:29
  • 1
    Which part of your task are you having problems with specifically? If your question is "how do I do multi-threading in Python", I would recommend any of the many tutorials and how-to's available online, that question is too broad for StackOverflow (and basically just asking people to rewrite such a tutorial, or refer to one, which is not what SO is for). Please give it a try yourself and if you're having trouble to get it to work, feel free to ask here. – Grismar Jan 01 '22 at 02:26
  • 1
    REMEMBER that if you have four threads printing something to the screen, the outputs of the threads will be intermingled. That's going to make your files unintelligible. – Tim Roberts Jan 01 '22 at 02:41
  • @Grismar I try to do it Several times but not working with me as I want : `import threading from datetime import datetime time_elapsed = datetime.now() - start_time def read_file(): f = open('Alice-In-Wonderland.txt') for line in f: print(line.strip() ,' : ', threading.current_thread().getName()) if __name__ == '__main__': threads = [] for i in range(15): t = threading.Thread(target=read_file) threads.append(t) t.start() time_elapsed = datetime.now() - start_time print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))` – Badr Khaled Jan 01 '22 at 03:49
  • @TimRoberts , Yes Sir, I want to calculate the Elapsed Time of Execution by Reading 4 text files using Several Methods: 1- To read from multiple text files (at least 4) sequentially and print their content on the screen. 2- Repeat [1] but use multi-threaded program, where each thread opens a different file, read its content, and print it on the screen. 3- Repeat [2] but this time do not print the file’s contents on the screen, write their content in a different file , here I will make 5 threads, 4 for reading from text files and the 5th to write from the pervious four threads in new file. – Badr Khaled Jan 01 '22 at 04:02
  • 1
    What are you going to learn? Reading a file is easy and takes almost no time. Writing to the console is very time-consuming, and 4 threads each writing a file is going to take about the same time as 1 thread writing 4 files -- the console is the bottleneck. – Tim Roberts Jan 01 '22 at 05:48

1 Answers1

1

Here's a code snippet that creates four threads to print the contents of four files (the comments have addressed the timeit module already, so I've treated the timing issue as resolved):

import pandas as pd
import threading

def print_text(filename):
    text = pd.read_csv(filename, delimiter = "\t")
    print(text)

if __name__ == "__main__":
    filenames = ["test1.txt", "test2.txt", "test3.txt", "test4.txt"]

    # Create thread for each filename.
    threads = [threading.Thread(target=print_text, args=(filename,)) for filename in filenames]

    # Start execution of each thread.
    for thread in threads:
        thread.start()
    
    # Join threads when execution is complete.
    for thread in threads:
        thread.join()
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33