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

Function read_jsonl_multithread

tools/io_file.py:321–348  ·  view source on GitHub ↗

使用多线程读取大JSONL文件。 参数: - file_path (str): 文件路径。 - chunk_size (int): 文件大小阈值,单位为字节。 - num_lines (int): 文件行数。 - encoding (str): 文件编码格式,默认为 'utf-8'。 返回: - list: 读取的JSONL文件内容。

(file_path, chunk_size, num_lines, encoding='utf-8')

Source from the content-addressed store, hash-verified

319
320
321def read_jsonl_multithread(file_path, chunk_size, num_lines, encoding='utf-8'):
322 """
323 使用多线程读取大JSONL文件。
324
325 参数:
326 - file_path (str): 文件路径。
327 - chunk_size (int): 文件大小阈值,单位为字节。
328 - num_lines (int): 文件行数。
329 - encoding (str): 文件编码格式,默认为 'utf-8'
330
331 返回:
332 - list: 读取的JSONL文件内容。
333 """
334 chunks = []
335 lines_per_chunk = num_lines // (os.cpu_count() // 2) # 使用一半CPU核心数
336
337 cpu_count = os.cpu_count() // 2 # 限制使用一半的CPU核心数
338 num_threads = min(cpu_count, (num_lines // lines_per_chunk) + 1)
339
340 with ThreadPoolExecutor(max_workers=num_threads) as executor:
341 futures = [
342 executor.submit(read_jsonl_chunk, file_path, i * lines_per_chunk, (i + 1) * lines_per_chunk, encoding)
343 for i in range(num_threads)
344 ]
345 for future in tqdm(futures, desc="读取JSONL文件进度"):
346 chunks.append(future.result())
347
348 return [item for sublist in chunks for item in sublist]
349
350
351def read_jsonl_chunk(file_path, start_line, end_line, encoding='utf-8'):

Callers 1

read_jsonlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected