4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

elixirのiex停止コマンドについて確認してみた

4
Posted at

IExのドキュメントを見直していて、ElixirのREPLであるiexから抜けるコマンドに、今まで知らなかったコマンドが追加されていたので、実際に動かして違いを確認してみました。

ここに書かれていた方法は3つ。

  • via the BREAK menu (available via Ctrl+C) by typing q, pressing enter
  • by hitting Ctrl+C, Ctrl+C
  • by hitting Ctrl+\

このうち、3つ目の「 Ctrl+\ 」が気になったので動作確認してみた。
ちなみに、3つ目はマニュアルを遡ってみると、v1.5.0-rcから採用されている様子。(記載時期は割と古かったようです...)

サンプルコード

1秒ごとにカウントを標準出力するプログラムを作成。
実行中にコマンドを打つことを想定しています。

sample.ex
defmodule Sample1 do

  def work do
    greet(:start)

    1..60
    |> Enum.map(fn x -> x end)
    |> greet_sleep()

    greet(:end)
  end

  defp greet(:start), do: IO.puts "start..."
  defp greet(:end), do: IO.puts "...end"
  defp greet(_), do: IO.puts "..."

  def greet_sleep([]), do: IO.puts ".....!"
  def greet_sleep([ head | tail ]) do
    IO.puts "...#{head}!"
    Process.sleep(1000)
    greet_sleep(tail)
  end
end

動作確認

プログラム実行中に、iex停止コマンドを入力してみます。

以前から知っていた上の二つのコマンドは、一旦メニューが出た後に処理を選択するタイプ。なので、「やっぱり続けたい」となった場合には、継続することもできます。下記では、「c」(※ (c)ontinue)で継続した後で停止しています。

$iex sample.ex 
Erlang/OTP 22 [erts-10.4.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Sample1.work
start...
...1!
...2!
...3!
...4!

BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
c
...5!
...6!
...7!

BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
^C$

一方、3つ目の「 Ctrl+\ 」はiexが即終了となります。
なので、さっさと処理を止めたい場合には、「 Ctrl+\ 」の方が便利です。

$iex sample.ex 
Erlang/OTP 22 [erts-10.4.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Sample1.work
start...
...1!
...2!
...3!
$

きになるのは、停止した時点のプロセスです。
とはいえ、今回のサンプルは、iex経由で起動したプロセスなので、iexが停止したら一緒に停止していると思われます。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?