【问题标题】:elixir mix app cannot understand clearlyelixir mix app看不清楚
【发布时间】:2017-10-24 09:49:43
【问题描述】:

我尝试使用长生不老药。

application.ex 有点难理解

defmodule PluralsightTweet.Application do
  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    # Define workers and child supervisors to be supervised
    children = [
      # Starts a worker by calling: PluralsightTweet.Worker.start_link(arg1, arg2, arg3)
       worker(PluralsightTweet.TweetServer, [])
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: PluralsightTweet.Supervisor]
    process = Supervisor.start_link(children, opts)
    PluralsightTweet.Scheduler.schedule_file("* * * * *", Path.join("#{:code.priv_dir(:pluralsight_tweet)}",
    "sample.txt"))
    process
  end
end

我正在关注复数灵药教程 这是从读取文本文件中每分钟发送文本的调度程序

任务是成功的,但对过程没有清晰的理想

谁能解释一下 application.ex 内部发生了什么 作为主管应用程序运行

【问题讨论】:

标签: erlang elixir erlang-otp


【解决方案1】:

use Application

此行表示当前模块是应用程序的入口。此类模块可以在mix.exs中配置为一个单元启动。

# Inside mix.exs
def application do
  [
    extra_applications: [:logger],
    mod: {PluralsightTweet.Application, []}  # <-- this line
  ]
end

start 函数

此函数是应用程序启动时的回调。您可以将其视为某些其他语言中的main 函数。

import Supervisor.Spec, warn: false

它只是让你在调用workersupervisorsupervise 时省略模块名称。即使您不调用任何这些函数,warn: false 部分也会抑制警告。

children = [worker(PluralsightTweet.TweetServer, [])]

此行指定您的应用程序监督的子进程。请注意,此时尚未生成子进程。

worker(mod, args) 只是定义了一个稍后将启动的工作规范。 args 将在启动 worker 时传递给 modstart_link 函数。

opts = [strategy: :one_for_one, name: PluralsightTweet.Supervisor]

主管选项。

strategy: :one_for_one的含义见strategies documentation及其他策略。

由于您只有一名工作人员,因此除:simple_one_for_one 之外的所有策略都几乎相同。

棘手的部分是name: PluralsightTweet.Supervisor。您可能想知道模块PluralsightTweet.Supervisor 是从哪里来的。事实是它不是一个模块。它只是一个原子:"Elixir.PluralsightTweet.Supervisor",作为主管进程的名称。

Supervisor.start_link(children, opts)

现在生成了主管进程及其子进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-24
    • 2020-11-15
    • 2013-02-28
    • 2020-01-29
    • 1970-01-01
    • 2017-07-26
    • 2018-11-30
    • 2022-08-22
    相关资源
    最近更新 更多