首页 > 解决方案 > 高效地并行运行 Pyspark 函数

问题描述

我有一个具有 3 个功能的 pyspark 代码。第一个函数是加载一些数据并为其他两个函数做准备。其他两个函数获取此输出并执行一些任务并生成相应的输出。

所以代码看起来像这样,

def first_function():
    # load data
    # pre-process
    # return pre-processed data

def second_function(output_of_first_function):
    # tasks for second function
    # return output

def third_function(output_of_first_function):
    # tasks for third function
    # return output

这些函数是从这样的主函数调用的,

def main():
    output_from_first_function = first_function()
    output_from_second_function = second_function(output_from_first_function)
    output_from_third_function = third_function(output_from_first_function)

second_function 和 third_function 之间没有相互依赖关系。我正在寻找一种同时并行运行这两个功能的方法。这些函数内部发生了一些转换。因此,并行帮助这些功能可能会有所帮助。

如何并行运行 second_function 和 third_function?这些函数中的每一个应该创建自己的火花上下文还是可以共享火花上下文?

标签: pythonpyspark

解决方案


From your problem, it doesn't seems like you really need pyspark. I think you should consider using Python Threads library. As described in this post: How to run independent transformations in parallel using PySpark?


推荐阅读