使用特定的samp并行读取多个文件

2024-04-20 05:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有10个CSV文件和百万条记录。我想并行读取10个文件,但是要以特定的速率(每5秒读取10个记录)。有效的方法是什么?。我使用的是Windows,以防有人建议使用OS调度器


Tags: 文件csv方法os速率windows记录调度
2条回答

我想试试joblib 下面是一些未经测试的示例代码。。。你知道吗

from joblib import Parallel, delayed
import time

#make a function that takes ONE filename and processes it the way you want
def process_csv(filename):
    count = 0
    #open file using method of choice plain file or csv
    f = open(filename)
    for line in f: 
        #do we ignore header?
        if count == 0:
            count += 1  
            continue
        arr = line.strip().split(',')#use csv module if not simple text
        #do something, store it, whatever

        if count % 10 == 0:
            time.sleep(5)

    return 1


if __name__ == '__main__': #windows-only protection

    filenames = ['file1', 'file2', ..., 'file10']

    dummy = Parallel(n_jobs=10)(delayed(process_csv)(fn) for fn in filenames)

现在,请注意,如果有人在您读取这些文件时添加到这些文件中…此代码可能无法工作。你知道吗

  1. 打开10个文件。你知道吗
  2. 从每个文件中读取1条记录(或10条,问题不清楚)。你知道吗
  3. 使用这些记录。你知道吗
  4. 等待当前的5秒间隔结束。你知道吗
  5. 转到2。你知道吗

相关问题 更多 >