Skip to content

Conversation

@nickrobinson251
Copy link
Contributor

@everywhere has (slightly surprising) behaviour for using/import statements that can best be avoided by using remotecall_eval directly, so i think remotecall_eval should be exported (or at least marked public but i think export is fine here.

Context:

The behaviour of @everywhere is that it tries to find using/import statements and run them on the calling process first, which means an @everywhere can't be defined inside a function if it contains a using/import (unless you add a hack to cause @everywhere to not find your using/import).

i.e. while this works

julia> module Foo end
Main.Foo

julia> @everywhere workers() begin
           using .Foo
       end

This fails:

julia> function f()
           @everywhere workers() begin
               using .Foo
           end
       end
ERROR: syntax: "toplevel" expression not at top level

An ugly hack to "hide" the using in a function call (lol this works):

julia> function f()
           @everywhere workers() begin
               print(using .Foo)
           end
       end
f (generic function with 1 method)

julia> f()

But a better solution is to just not use @everywhere, since this is it's documented behaviour:

  Similar to calling remotecall_eval(Main, procs, expr), but with two extra features:

    •  using and import statements run on the calling process first, to ensure packages are precompiled.

    •  The current source file path used by include is propagated to other processes.

and instead use remotecall_eval just as the docstring for @everywhere mentions, i.e.

remotecall_eval(Main, workers(), :(using .Foo))

@codecov
Copy link

codecov bot commented Jan 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.29%. Comparing base (0e27cd1) to head (2344bdd).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #123   +/-   ##
=======================================
  Coverage   79.29%   79.29%           
=======================================
  Files          10       10           
  Lines        1922     1922           
=======================================
  Hits         1524     1524           
  Misses        398      398           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nickrobinson251 nickrobinson251 force-pushed the npr-export-remotecall_eval branch from 8d17f0a to 2344bdd Compare January 24, 2025 10:45
Copy link
Member

@JamesWrigley JamesWrigley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@JamesWrigley JamesWrigley merged commit ff34f4c into JuliaLang:master Jan 24, 2025
8 checks passed
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 this pull request may close these issues.

2 participants