使用多处理模块时无法将 pandas 数据帧对象发送到 SQL

Unable to send pandas dataframe object to SQL when using multiprocessing module

我正在使用多个 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() 方法 and/or 在 function 中插入 return 语句的上述代码,并且在行为上没有差异。

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

您需要确保该函数可以访问所有变量,否则可能会出现静默故障。