I have an issue : Reading Multiple Text files using Multi-Threading by python

2024/7/6 21:21:08

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.
Answer

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 threadingdef 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()
https://en.xdnf.cn/q/119075.html

Related Q&A

How to print \ in python?

print "\\"It print me in console...But I want to get string \How to get string string \?

Replace word, but another word with same letter format got replaced

Im trying to replace a word in python, but another word with same letter format got replaced example : initial : bg bgt goal : bang banget current result : bang bangtheres what my code…

Python: Split timestamp by date and hour

I have a list of timestamps in the following format:1/1/2013 3:30I began to learn python some weeks ago and I have no idea how to split the date and time. Can anyone of you help me?Output should be on…

ModuleNotFoundError: when importing curses in IDE

I get the error ModuleNotFoundError: No module named _curses every time I try to uses curses in VS Code or PyCharm. But it works in the command prompt (Im on Windows BTW) Code is from Tech With Tim tut…

Add new column in a csv file and manipulate on the on records

I have 4 csv files named PV.csv, Dwel.csv, Sess.csv, and Elap.csv. I have 15 columns and arouind 2000 rows in each file. At first I would like to add a new column named Var in each file and fill up the…

Xpath returns null

I need to scrape the price of this page: https://www.asos.com/monki/monki-lisa-cropped-vest-top-with-ruched-side-in-black/prd/23590636?colourwayid=60495910&cid=2623 However it is always returning …

I am getting an Index error as list out of range. I have to scan through many lines

import nltk import random from nltk.tokenize import sent_tokenize, word_tokenizefile = open("sms.txt", "r") for line in file:#print linea=word_tokenize(line)if a[5] == SBI and a[6]=…

How can i append two classes in JQuery

I have implemented a chat box message (live chat) using django and now i want to add css, but i have problem on how to append multiple classes on messege sent. For example, i want to show other user me…

After installing PyBluez on Windows8.1 I get DLL %1 not valid win32 app

I have installed PyBluez-0.22.win32.exe on a 64bit machine with Python 2.7 (they didnt have a 64bit version). Then I get the following error: ImportError:DLL load failed:%1 is not valid Win32 applicati…

Flask Apache on AWS EC2 - Read/Write Failing

So Ive been stumped by this problem for a day now. Im relatively new to AWS EC2 so have been experimenting with Python Flask apps on it.I have an Ubuntu instance, and can get a flask app to run fine on…