栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python多进程处理数据

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python多进程处理数据

背景

假设有1千万任务数据,每条数据处理耗时1s,那么如何加速整个任务的处理速度?其中一种解决方案就是使用多进程处理。

解决方案

pandas + pandarallel
安装pandarallel: pip install pandarallel

示例
# -*- coding: utf-8 -*-
# @Time    : 2022/5/21 6:14 下午
# @Author  : JasonLiu
# @FileName: test.py
import time
import pandas as pd
from pandarallel import pandarallel
pandarallel.initialize(nb_workers=5)
text1 = ["华中科技大学"+str(i) for i in range(10)]

task_df = pd.DataFrame({"text1": text1})


def text_processing(text):
    """
    做一些文本的处理操作, 这里仅仅是为了演示
    """
    text += " HUST"
    time.sleep(2)
    return text


start_time = time.time()
task_df["new_text1"] = task_df["text1"].apply(text_processing)
end_time = time.time()
print("raw apply cost=", end_time-start_time)
print(task_df)
start_time = time.time()
task_df["new_text2"] = task_df["text1"].parallel_apply(text_processing)
end_time = time.time()
print("parallel_apply cost=", end_time-start_time)
print(task_df)
运行结果如下:
INFO: Pandarallel will run on 5 workers.
INFO: Pandarallel will use Memory file system to transfer data between the main process and workers.
raw apply cost= 20.01844358444214
     text1     new_text1
0  华中科技大学0  华中科技大学0 HUST
1  华中科技大学1  华中科技大学1 HUST
2  华中科技大学2  华中科技大学2 HUST
3  华中科技大学3  华中科技大学3 HUST
4  华中科技大学4  华中科技大学4 HUST
5  华中科技大学5  华中科技大学5 HUST
6  华中科技大学6  华中科技大学6 HUST
7  华中科技大学7  华中科技大学7 HUST
8  华中科技大学8  华中科技大学8 HUST
9  华中科技大学9  华中科技大学9 HUST
parallel_apply cost= 4.040616035461426
     text1     new_text1     new_text2
0  华中科技大学0  华中科技大学0 HUST  华中科技大学0 HUST
1  华中科技大学1  华中科技大学1 HUST  华中科技大学1 HUST
2  华中科技大学2  华中科技大学2 HUST  华中科技大学2 HUST
3  华中科技大学3  华中科技大学3 HUST  华中科技大学3 HUST
4  华中科技大学4  华中科技大学4 HUST  华中科技大学4 HUST
5  华中科技大学5  华中科技大学5 HUST  华中科技大学5 HUST
6  华中科技大学6  华中科技大学6 HUST  华中科技大学6 HUST
7  华中科技大学7  华中科技大学7 HUST  华中科技大学7 HUST
8  华中科技大学8  华中科技大学8 HUST  华中科技大学8 HUST
9  华中科技大学9  华中科技大学9 HUST  华中科技大学9 HUST

从中可以看出,使用 Pandarallel 使得整个处理耗时从原始的20s,缩减到4s。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/920470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号