多线程问题:我想添加一个额外的线程来写入并将已经从 4 个文本文件中读取的 4 个线程保存在一个新的文本文件中
Multithreading issue : I want add an extra thread to write and save that 4 threads that already read from 4 text files in a new text file
我有 4 个线程读取 4 个文本文件并将其打印在屏幕上。我想创建额外的线程来写入并保存在新文本文件中读取的 4 个线程。
当前代码:
import pandas as pd
import threading
from datetime import datetime
start_time = datetime.now()
def print_text(filename):
text = pd.read_csv(filename, delimiter = "\t")
print(text)
if __name__ == "__main__":
filenames = ['file1.txt', "file2.txt", "file3.txt", "file4.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()
time_elapsed = datetime.now() - start_time
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
这显示了如何使用队列将四个“编写器”线程的输出路由到“reader”线程。精明的 reader 会意识到为此生成第 5 个线程没有意义。我们可以在主线代码中调用 print_result
并以更低的开销获得相同的结果。
import queue
import threading
from datetime import datetime
start_time = datetime.now()
def print_text(q, filename):
for line in open(filename):
q.put(line.strip())
q.put('--end--')
def print_result(q, count=0):
while count:
line = q.get()
if line == '--end--':
count -= 1
else:
print(line)
if __name__ == "__main__":
filenames = ['file1.txt', "file2.txt", "file3.txt", "file4.txt"]
q = queue.Queue()
threads = [threading.Thread(target=print_text, args=(q, filename)) for filename in filenames]
threads.append( threading.Thread(target=print_result, args=(q, len(filenames))) )
# Start execution of each thread.
for thread in threads:
thread.start()
# Join threads when execution is complete.
for thread in threads:
thread.join()
time_elapsed = datetime.now() - start_time
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
我有 4 个线程读取 4 个文本文件并将其打印在屏幕上。我想创建额外的线程来写入并保存在新文本文件中读取的 4 个线程。 当前代码:
import pandas as pd
import threading
from datetime import datetime
start_time = datetime.now()
def print_text(filename):
text = pd.read_csv(filename, delimiter = "\t")
print(text)
if __name__ == "__main__":
filenames = ['file1.txt', "file2.txt", "file3.txt", "file4.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()
time_elapsed = datetime.now() - start_time
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
这显示了如何使用队列将四个“编写器”线程的输出路由到“reader”线程。精明的 reader 会意识到为此生成第 5 个线程没有意义。我们可以在主线代码中调用 print_result
并以更低的开销获得相同的结果。
import queue
import threading
from datetime import datetime
start_time = datetime.now()
def print_text(q, filename):
for line in open(filename):
q.put(line.strip())
q.put('--end--')
def print_result(q, count=0):
while count:
line = q.get()
if line == '--end--':
count -= 1
else:
print(line)
if __name__ == "__main__":
filenames = ['file1.txt', "file2.txt", "file3.txt", "file4.txt"]
q = queue.Queue()
threads = [threading.Thread(target=print_text, args=(q, filename)) for filename in filenames]
threads.append( threading.Thread(target=print_result, args=(q, len(filenames))) )
# Start execution of each thread.
for thread in threads:
thread.start()
# Join threads when execution is complete.
for thread in threads:
thread.join()
time_elapsed = datetime.now() - start_time
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))