| import gzip | |
| from concurrent.futures import ProcessPoolExecutor | |
| import random | |
| from tqdm.auto import tqdm | |
| MONTHS = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"] | |
| def process_month(month): | |
| train = gzip.open(f"train/2022-{month}.jsonl.gz", "wb") | |
| val = gzip.open(f"val/2022-{month}.jsonl.gz", "wb") | |
| test = gzip.open(f"test/2022-{month}.jsonl.gz", "wb") | |
| with gzip.open(f"all/2022-{month}.jsonl.gz", "rb") as f: | |
| for i, line in tqdm(enumerate(f)): | |
| rand = random.random() | |
| if rand <= 0.001: | |
| val.write(line) | |
| elif rand <= 0.002: | |
| test.write(line) | |
| else: | |
| train.write(line) | |
| train.close() | |
| val.close() | |
| test.close() | |
| ppe = ProcessPoolExecutor(12) | |
| for _ in ppe.map(process_month, MONTHS): | |
| ... | |
| ppe.shutdown() | |
| print("Done!") | |
| ppe.shutdown() | |
| print("Done!") | |