我正在使用多个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模块。我需要一个并行转换单个数据帧并将结果插入数据库的解决方案。在
您需要确保函数可以访问所有变量,否则可能会发生静默故障。在
相关问题 更多 >
编程相关推荐