我试图加快用python处理文件所需的时间。我的想法是把任务分成n个线程
例如,如果我有一个包含1300项的文件。我希望每个线程处理每n个项目。每件商品都不依赖于任何其他商品,所以订单在这里并不重要
所以每个线程的工作流都是这样的:
1) open file
2) iterate through items
3) if nth item then process, otherwise continue
我正在使用线程库来实现这一点,但没有看到任何性能改进
以下是伪代码:
def driver(self):
threads = []
# Just picked 10 as test so trying to create 10 threads
for i in range(0,10):
threads.append(threading.Thread(target=self.workerFunc, args=(filepath, i, 10)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def workerFunc(self, filepath):
with open(filepath, 'rb') as file:
obj = ELFFile(file)
for item in obj.items:
if (item is not nth item):
continue
else:
process this item
因为每个线程都只是在读取文件,所以它应该能够自由地扫描文件,而不必关心其他线程正在做什么或被它们阻止,对吗
我在这里看什么
我唯一能想到的是,我用来格式化这些文件的库(pyelftool ELFFile)有一些内部阻塞,但我找不到它。或者我的计划有什么根本性的缺陷
编辑:只是要注意,有32个CPU的系统上,我运行这个
目前没有回答
相关问题 更多 >
编程相关推荐