MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / write_csv_multithread

Function write_csv_multithread

tools/io_file.py:509–529  ·  view source on GitHub ↗

使用多线程写入大CSV文件。 参数: - file_path (str): 文件路径。 - data (DataFrame): 要写入的CSV数据。 - mode (str): 写入模式,默认为 'a'(追加模式),可以选择 'w'(覆盖模式)。 - chunk_size (int): 数据大小阈值,单位为字节。 - encoding (str): 写入文件的编码格式,默认为 'utf-8'。

(file_path, data, mode, chunk_size, encoding='utf-8')

Source from the content-addressed store, hash-verified

507
508
509def write_csv_multithread(file_path, data, mode, chunk_size, encoding='utf-8'):
510 """
511 使用多线程写入大CSV文件。
512
513 参数:
514 - file_path (str): 文件路径。
515 - data (DataFrame): 要写入的CSV数据。
516 - mode (str): 写入模式,默认为 'a'(追加模式),可以选择 'w'(覆盖模式)。
517 - chunk_size (int): 数据大小阈值,单位为字节。
518 - encoding (str): 写入文件的编码格式,默认为 'utf-8'
519 """
520 chunks = [data.iloc[i:i + chunk_size] for i in range(0, len(data), chunk_size)]
521 cpu_count = os.cpu_count() // 2 # 限制使用一半的CPU核心数
522 num_threads = min(cpu_count, len(chunks))
523
524 logger.info(f"分割数据为 {len(chunks)} 个块进行多线程写入")
525
526 with ThreadPoolExecutor(max_workers=num_threads) as executor:
527 futures = [executor.submit(write_csv_chunk, file_path, chunk, mode, encoding) for chunk in chunks]
528 for future in tqdm(futures, desc="写入CSV文件进度"):
529 future.result()
530
531
532def write_csv_chunk(file_path, chunk, mode, encoding='utf-8'):

Callers 1

write_csvFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected