我正在尝试编写一个能够对用户输入做出反应的简单elixir程序.我的问题是从stdio读取似乎不起作用.如果我的整个想法很愚蠢,请告诉我一个如何完成的例子.我在网上找不到任何东西
我把问题简化为一个简单的例子:
t = Task.async((fn->IO.gets "what?" end))
%Task{owner: #PID<0.65.0>, pid: #PID<0.80.0>, ref: #Reference<0.0.2.135>}
Run Code Online (Sandbox Code Playgroud)
任务开始:
iex(4)> pid=Map.get(t, :pid)
#PID<0.80.0>
iex(5)> Process.alive? pid
true
Run Code Online (Sandbox Code Playgroud)
并且它不会打印到stdio也不会读取.它没有正常退出或有异常.我也尝试过IO.read/2.
在我的程序中,Task以Task.spawn_link/1启动,但问题是相同的.IO.gets/2和IO.gets/2函数后面的代码没有执行.
主管开始任务:
defmodule Prime do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Define workers and child supervisors to be supervised
worker(Task, [fn->Prime.IO.communicate(nil) end], restart: :transient),
supervisor(Prime.Test.Supervisor, [])
]
opts = [strategy: :one_for_one, name: Prime.Supervisor]
Supervisor.start_link(children, opts) end end
Run Code Online (Sandbox Code Playgroud)
任务功能:
defmodule Prime.IO do
@doc """
handles communication with the user and …Run Code Online (Sandbox Code Playgroud)