Skip to content

Commit 3c28c5e

Browse files
committed
fixup! Document how to support different distributed worker backends
1 parent 1c1e366 commit 3c28c5e

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

docs/src/support-preferences.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,43 @@ that they are not compatible at all. E.g. you cannot
77
you as a package developer want to make your package support both Distributed
88
and DistributedNext, we suggest using
99
[Preferences.jl](https://juliapackaging.github.io/Preferences.jl/stable/) to
10-
choose which library to load:
10+
choose which package to load.
11+
12+
Here's an example for a package named Foo.jl:
1113
```julia
12-
import Preferences: load_preference
14+
module Foo
15+
16+
import Dependency
17+
import Preferences: @load_preference, @set_preferences!
1318

14-
const distributed_library_name = load_preference("distributed-library", "name")
15-
if distributed_library_name == "DistributedNext"
19+
const distributed_package = @load_preference("distributed-package")
20+
if distributed_package == "DistributedNext"
1621
using DistributedNext
17-
elseif distributed_library_name == "Distributed"
22+
elseif distributed_package == "Distributed"
1823
using Distributed
1924
else
20-
error("Unsupported `distributed-library`: '$(distributed_library_name)'")
25+
error("Unsupported `distributed-package`: '$(distributed_package)'")
2126
end
22-
```
2327

24-
`distributed-library` is a top-level name that we recommend all packages who
25-
want to support switching backends use. The advantage of using a global
26-
preference instead of a per-package preference is that you can set it once:
27-
```julia
28-
import Preferences: set_preferences!
28+
"""
29+
set_distributed_package!(value[="Distributed|DistributedNext"])
2930
30-
set_preferences!("distributed-library", "name" => "DistributedNext")
31+
Set a [preference](https://github.com/JuliaPackaging/Preferences.jl) for using
32+
either the Distributed.jl stdlib or DistributedNext.jl. You will need to restart
33+
Julia after setting a new preference.
34+
"""
35+
function set_distributed_package!(value)
36+
# Set preferences for all dependencies
37+
Dependency.set_distributed_package!(value)
38+
39+
@set_preferences!("distributed-package" => value)
40+
@info "Foo.jl preference has been set, restart your Julia session for this change to take effect!"
41+
end
42+
43+
end
3144
```
3245

33-
And all packages that support the `distributed-library` preference will pick
34-
that up automatically so an end-user doesn't need to worry about setting
35-
preferences for all of the dependencies of the package that they want to use.
46+
Users will then be able to call
47+
e.g. `Foo.set_distributed_package!("DistributedNext")`. Note that
48+
`Foo.set_distributed_package!()` should also set the preferences of any dependencies
49+
of Foo.jl that use a distributed worker package.

0 commit comments

Comments
 (0)