使用多处理modu时无法将pandas dataframe对象发送到SQL

2024-04-24 06:45:27 发布

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

我正在使用多个cpu内核转换单个数据帧,并希望将结果插入MySQL。在

使用下面的代码,我只观察到一个活动的cpu内核,没有对MySQL进行更新。未生成错误消息。在

原始数据帧pandas_df永远不会更改。对pandas_df的所有转换都存储在result_df中。在

经验证,该代码串行工作正常。在

import multiprocessing as mp
from sqlalchemy import create_engine
engine = create_engine(MYSQL_STRING)

def function(pandas_df, tuple, engine):
    #slice and dice pandas_df according to tuple
    result_df.to_sql("TABLE_NAME", engine, if_exists='append')


pool = mp.Pool(processes=4)
for tuple in tuples:
    pool.apply_async(est, args=(pandas_df, tuple, engine))

我遇到的大多数教程和指南只在args=()中传递字符串。 不过,也有一些文章演示了传递numpy数组的能力:http://sebastianraschka.com/Articles/2014_multiprocessing_intro.html

我还尝试过使用map_async()方法和/或在function内插入return语句的上述代码,在行为上没有任何区别。在

我愿意尝试不同的python模块。我需要一个并行转换单个数据帧并将结果插入数据库的解决方案。在


Tags: 数据代码importpandasdfcreatemysqlfunction