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.
CodePudding user response:
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()