Loading code in worker processes

Previously I’ve always specified the number of worker processes on the Julia command line via -p . Since the source file passed in is the same for all the processes, I can use Distributed.remotecall_fetch() for any function available to the main process.

Now I’m trying to add workers dynamically, via addproc from julia_main() in program.jl (the entry-point to my application). I’d like to be able to do the equivalent of @everywhere import MyModule once the workers have been added (in julia_main()), but ‘import’ can only be used at the top level. So far, the solution I’ve come up with is to do @everywhere include("/path/to/program.jl") but this seems rather ugly. Is there a cleaner way? Thanks.


eval() and @eval always execute at the top level (and in global scope). Does @eval import MyModule work for your case?

Yep, that does exactly what I need. This also works with static compilation which failed with the @everywhere include hack. Thanks a bunch.

An interesting wrinkle is that if one attempts to combine the module and the application driver in the same file, this fails, because the ``@everywhere @eval import Module``` occurs in the file defining the module, hence it is already defined in the main process.

I tried a number of different workarounds, but the best I could come up with was:

        @everywhere if !isdefined(Main, :Module) # Shouldn't import into the main worker
            @eval import Module