Julia中使用数据帧的并行处理

Julia中使用数据帧的并行处理,julia,Julia,我参考文档中的示例进行处理,并尝试根据我的用例对其进行调整。在本例中的每个独立迭代中,我都会得到一个DataFrame,最终需要使用vcat()在所有迭代中进行组合。这是我迄今为止尝试的简化版本: using DataFrames, Distributed function test() if length(workers()) < length(Sys.cpu_info()) addprocs(length(Sys.cpu_info()); exeflags="

我参考文档中的示例进行处理,并尝试根据我的用例对其进行调整。在本例中的每个独立迭代中,我都会得到一个
DataFrame
,最终需要使用
vcat()
在所有迭代中进行组合。这是我迄今为止尝试的简化版本:

using DataFrames, Distributed

function test()
    if length(workers()) < length(Sys.cpu_info())
        addprocs(length(Sys.cpu_info()); exeflags="--project=" * Base.active_project())
    end

    nheads = @distributed (vcat) for i = 1:20
        DataFrame(a=[Int(rand(Bool))])
    end
end
使用数据帧,分布式
功能测试()
如果长度(workers())
但是在运行
test()
时,我得到了错误:

错误:在辅助进程2上:未定义错误:未定义数据帧


我需要做什么来纠正这个问题?

您的
使用数据帧…
第一行的语句只适用于主“线程”。因此,您的工作线程没有导入所需的库

要解决此问题,应在第一行添加关键字
@everywhere
。这将要求所有进程导入这些库

编辑

刚刚注意到您在函数中执行了
addprocs
。那我的建议就行不通了。以下是一个工作版本:

using Distributed

addprocs(length(Sys.cpu_info()))

@everywhere using DataFrames

function test()
    nheads = @distributed (vcat) for i = 1:20
        DataFrame(a=[Int(rand(Bool))])
    end
end

test()

第一行的
使用数据帧…
语句仅适用于主“线程”。因此,您的工作线程没有导入所需的库

要解决此问题,应在第一行添加关键字
@everywhere
。这将要求所有进程导入这些库

编辑

刚刚注意到您在函数中执行了
addprocs
。那我的建议就行不通了。以下是一个工作版本:

using Distributed

addprocs(length(Sys.cpu_info()))

@everywhere using DataFrames

function test()
    nheads = @distributed (vcat) for i = 1:20
        DataFrame(a=[Int(rand(Bool))])
    end
end

test()

我尝试了你的建议,但现在我得到了错误:评估错误:mod=Main,ex=@everywhere使用DataFrames,exception=type Nothing没有字段framedataHm。这对我有用。你用的是什么版本的Julia?是Julia 1.1.0。奇怪的是,当@everywhere using DataFrames在REPL中运行时,它可以工作,但不是通过上面的代码。进一步研究,只要DataFrames安装在默认的Julia环境中(在我的例子中是v1.1),代码就可以工作。如果您只在您使用的特定于项目的环境中安装了数据帧,那么它就不起作用。我尝试了您的建议,但现在我得到了错误:评估错误:mod=Main,ex=@where using DataFrames,exception=type Nothing没有字段framedataHm。这对我有用。你用的是什么版本的Julia?是Julia 1.1.0。奇怪的是,当@everywhere using DataFrames在REPL中运行时,它可以工作,但不是通过上面的代码。进一步研究,只要DataFrames安装在默认的Julia环境中(在我的例子中是v1.1),代码就可以工作。如果仅在您使用的特定于项目的环境中安装了数据帧,那么它将不起作用。用环境加载进程的另一种方法是以以下方式启动julia:julia--project=“path\u to\u environment”-e”,使用Distributed;addprocs(length(Sys.cpu\u info());exefagas=\“--project=\”*Base.active\u project());include(\'path\'u to\'u file\'u to\'u run.jl\”;“要使用答案中的代码运行文件,而不使用addprocs()行,另一种在环境中加载进程的方法是以以下方式启动julia:julia--project=“path\'u to\'u environment”-e”使用分布式;addprocs(length(Sys.cpu\'u info());exefagas=\“--project=\”*Base.active\u project());include(\'path\'u to \'u file\'u to \'u run.jl\”;;使用答案中的代码运行文件,而不使用addprocs()行