我厌倦了使用GenServer
作为工人:
defmodule CSVServer do
use GenServer
def start_link(_opts \\ []) do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
def process!(csvs_params), do: GenServer.call(__MODULE__, %{csvs_params: csvs_params})
def handle_call(%{csvs_params: csvs_params}, _from, state) do
...
end
在我的应用程序中:
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Start the endpoint when the application starts
supervisor(MyApp.Endpoint, []),
# Start the Ecto repository
supervisor(MyApp.Repo, []),
worker(CSVServer, [], name: "CSVServer"),
]
问题是,我是否必须明确指定工人?于:
worker(CSVServer, [], name: "CSVServer"),
它已经在GenServer
命名为:
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
修改
在生成的myapp.ex
中,我可以看到:
# Here you could define other workers and supervisors as children
# worker(MyApp.SomeWorker, [], name: SomeWorker),