【发布时间】:2018-11-17 05:47:41
【问题描述】:
我在模块中有一个函数,我想在其中动态更改进程数 (addproc)。在我启动这些新过程之后,我想添加一个要在每个工作人员上执行的函数。但是,我在模块内部使用 @everywhere 定义此函数时遇到了困难。我只想在函数do_work 中生成这些新进程。我不想在导入/使用模块之前生成新进程(这对于多线程来说是一项理想的工作,但我的函数会执行大量 I/O,而我目前在 Julia 1.0 中遇到了段错误)。
这是我的问题的一个简单的工作示例。有没有办法在工作进程上定义foo(最好不调用@everywhere include("somefile_with_foo.jl"))
module Example
using Distributed
function do_work()
total_procs = nprocs()
# Make sure we have as many works are cores
if total_procs < Sys.CPU_THREADS
addprocs(Sys.CPU_THREADS - total_procs);
end
@everywhere function foo(x::Integer)
println(x)
end
remotecall(foo, 2, 2)
while nprocs() > total_procs
rmprocs(procs()[end])
end
end
end
【问题讨论】:
标签: parallel-processing multiprocessing thread-safety julia