Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surprising and undocumented import handling in Distributed.@everywhere #65

Open
mwallerb opened this issue Nov 11, 2019 · 2 comments
Open

Comments

@mwallerb
Copy link

Consider the following code:

    using Distributed
    addprocs(1)
    @everywhere begin
          # assume MyModule is defined in the path given
          push!(LOAD_PATH, "/path/to/my/module")
          import MyModule
    end

This fails with a LoadError, because @everywhere cherry-picks the imports for each everywhere block and runs them first regardless of the order of statements inside the block. Instead, one has to do this:

    # (...)
    @everywhere push!(LOAD_PATH, "/path/to/my/module")
    @everywhere import MyModule

This behaviour thoroughly breaks my internal parser, because these two blocks look equivalent (modulo an unnecessary synchronization point in the middle). Worse, the behaviour is not documented, and in fact contradicts the documentation.

Unfortunately, no rationale is given in the code as to why this was chosen. However, I think at the very least, one should amend the documentation with a corresponding note and add a rationale for this in the code.

One could maybe also issue a warning for @everywhere blocks which do not have import statements as their first statements. This would help poor newbies like me :)

Julia version: 1.2.0 (also present in master)

@vchuravy
Copy link
Member

x-ref: JuliaLang/julia#21718

@joseortiz3
Copy link

Thank you!! I was getting very strange errors LoadError and UndefVarError in my worker processes, as if modules I loaded in

@everywhere begin
    import Random
    using Printf
    using GraphPlot
    using JuMP
    import CSV
    using Profile

    <other non-import-related code here>
end

were not actually loaded at all. Putting @everywhere at the beginning of just the import and using lines fixed it. All my other code (functions, variables, etc) was fine inside a single @everywhere begin .... end block. Thanks for pointing this out, hope others find this useful.

@vtjnash vtjnash transferred this issue from JuliaLang/julia Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants