有队列的文件块处理?

时间:2019-05-10 20:10:43

标签: python multithreading file asynchronous buffer

我正在寻找一种处理块中的文件以将其传递给线程的方法。我不确定最简单的方法是什么。

这是我想要实现的伪代码。

让我们说我们有一个大文件


file = open("file.txt", "rb")
# then read file for example  30 line at a time
# so each chunk is for example 30 line
f = file.readlines()
chunk1 = f[0:30]
chunk2 = f[30:60]
#etc

现在我想传递每个块以使用类似的线程对其进行处理

while not eof: 
    threading.process(chunk[i] , 30) #30 here is number of threads i want use to process each chunk

所以基本上我试图将文件读取为块,然后使用n个线程处理每个块。什么是实现这一目标的最佳方法?可能要排队吗?

我尝试使用嵌套循环,但似乎不合适

for chunk in chunks:
        for i in range(thread-numbers):
              #process-chunk
              ....

i除了读取块并使用线程分别处理它们外。希望我的问题有意义。

谢谢大家

0 个答案:

没有答案