【发布时间】:2016-01-30 23:34:37
【问题描述】:
我有一个非常大的文件,~1Gig,我想运行并行读取部分文件的线程。
NUM_THREADS = 50
FILE_NAME = "some/file"
def read_chunk(offset, lines_to_read):
# Read 'lines_to_read' number of lines from FILE_NAME, starting at 'offset'
def divide_work():
num_lines = sum(1 for line in open(FILE_NAME))
lines_per_thread = math.ceil(num_lines/NUM_THREADS))
for i in range (0, NUM_THREADS):
offset = i * lines_per_thread
thread = Thread(target = read_chunk, args = (offset, lines_per_thread,))
thread.start()
thread.join()
假设文件中的行数将平均分为线程数。如何从某个行偏移中读取一定数量的行?我知道'seek',但它使用字节,我需要使用行。
【问题讨论】:
-
到单个驱动器的 IO 通常不能并行化。查找消费者生产者模式。
-
如果人们对您的问题投了反对票,但没有提供他们为什么这样做的反馈,那就太好了。
标签: python multithreading io