我正在努力第一次实现一个主管,我遇到了一些我无法从文档中找到的问题.具体来说,当我尝试使用SlowRamp.flood我开始我的过程{:error, {:invalid_child_spec, []}}.
这是一个非常简单的应用程序,使用mix new slow_ramp --sup.
主要文件./lib/slow_ramp.ex是:
defmodule SlowRamp do
use Application
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(SlowRamp.Flood, [])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: SlowRamp.Supervisor]
Supervisor.start_link(children, opts)
end
def flood do
Supervisor.start_child(SlowRamp.Supervisor, [])
end
end
Run Code Online (Sandbox Code Playgroud)
我的子函数/文件在./lib/SlowRamp/flood.ex,看起来像这样:
defmodule SlowRamp.Flood do
def start_link do
Task.start_link(fn -> …Run Code Online (Sandbox Code Playgroud) 在 Elixir(或 Erlang)中,如果我有一个内存文件,我如何找到它的长度(以字节为单位)?
{:ok, fd} = :file.open("", [:ram, :read, :write])
:file.write(fd, "hello")
Run Code Online (Sandbox Code Playgroud) 我有调试复杂递归函数的问题我正在使用这个成语:
dbg:tracer(),dbg:p(all,c),dbg:tpl(Mod,Fun1,x),dbg:tpl(Mod,Fun2,x)...
Run Code Online (Sandbox Code Playgroud)
这给了我一个对所有函数调用的平面列表,其中很难找出哪个返回属于哪个调用.
是否有一种简单的方法可以使其更具可读性,例如通过缩进.
我可以只为每次调用发布生成和缩进的文本进行处理,并且每次返回都是outdent,但这听起来不是很优雅.
这是我的代码,当我运行:gen_server_test:alloc,它给了我错误,请帮帮我
-module(gen_server_test).
-behaviour(gen_server).
-export([start_link/0]).
-export([alloc/0, free/1]).
-export([init/1, handle_call/3, handle_cast/2]).
start_link() ->
gen_server:start_link({local, gen_server_test}, gen_server_test, [], []).
alloc() ->
io:format("allocing"),
gen_server:call(?MODULE, {alloc}).
free(Ch) ->
gen_server:cast(gen_server_test, {free, Ch}).
init(_Args) ->
{ok, channels()}.
handle_call({alloc}, _From, LoopData) ->
io:format("handle alloc").
handle_cast({free, Ch}, LoopData) ->
io:format(Ch).
free() ->
io:format("free").
channels() ->
io:format("channels").
Run Code Online (Sandbox Code Playgroud)
错误:
23> gen_server_test:alloc().
allocinghandle alloc
=ERROR REPORT==== 6-May-2011::18:05:48 ===
** Generic server gen_server_test terminating
** Last message in was {alloc}
** When Server state == ok
** Reason for termination ==
** {'function …Run Code Online (Sandbox Code Playgroud) 我在erlang中编写了一个服务器应用程序,在C#中编写了一个客户端 它们通过3个TCP端口进行通信.端口号是硬编码的.现在我想动态地这样做.这是我第一次进行网络编程,所以请原谅我无法使用正确的术语:-D
我想做的是让一个主管接受来自客户端在先前已知端口(例如,10000或其他)上的TCP连接,然后找到3个空闲端口,在这3个端口上启动服务器应用程序并告诉客户端那些端口号,以便客户端可以连接到服务器.
我的特殊问题是:如何找到3个未使用的端口?(澄清:用哪个模块:fun()来查找自由端口?)
我的一般问题是:我确定这种东西(一个服务器分配端口和重定向客户端)是相当常见的网络编程问题,应该有一堆(特定于erlang或一般)资源,但我只是不要没有谷歌的术语.
我使用以下代码在elixir上创建TCP侦听器:
defmodule KVServer do
use Application
@doc false
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(Task.Supervisor, [[name: KVServer.TaskSupervisor]]),
worker(Task, [KVServer, :accept, [4040]])
]
opts = [strategy: :one_for_one, name: KVServer.Supervisor]
Supervisor.start_link(children, opts)
end
@doc """
Starts accepting connections on the given `port`.
"""
def accept(port) do
{:ok, socket} = :gen_tcp.listen(port,
[:binary, packet: :line, active: false, reuseaddr: true])
IO.puts "Accepting connections on port #{port}"
loop_acceptor(socket)
end
defp loop_acceptor(socket) do
{:ok, client} = :gen_tcp.accept(socket)
{:ok, pid} = Task.Supervisor.start_child(KVServer.TaskSupervisor, fn -> serve(client) …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何在Elixir类型和函数规范中组合参数化类型和类型变量.举个简单的例子,假设我正在定义一个Stack模块:
defmodule Stack do
@type t :: t(any)
@type t(value) :: list(value)
@spec new() :: Stack.t
def new() do
[]
end
# What should the spec be?
def push(stack, item) do
[item|stack]
end
end
Run Code Online (Sandbox Code Playgroud)
使用第3行的参数化类型规范,我可以定义一个函数来创建一个只应包含整数的新堆栈:
@spec new_int_stack() :: Stack.t(integer)
def new_int_stack(), do: Stack.new
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在我想确保只有整数可以推入这个堆栈.例如,透析器应该没问题:
int_stack = new_int_stack()
Stack.push(int_stack, 42)
Run Code Online (Sandbox Code Playgroud)
但是透析器应该抱怨这个:
int_stack = new_int_stack()
Stack.push(int_stack, :boom)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚push函数的类型规范应该强制执行什么.在Erlang中,我很确定这种语法可以解决这个问题:
-spec push(Stack, Value) -> Stack when Stack :: Stack.t(Value).
Run Code Online (Sandbox Code Playgroud)
有没有办法用Elixir表达这个约束@spec?
我刚刚读完Elixir.
生成一个hmac哈希后,我得到了一个bitstring:
:crypto.hmac(:sha512, secret, data)
Sign: <<104, 155, 224, 193, 121, 129, 237, 103, 233, 236, 161, 130...>>
Run Code Online (Sandbox Code Playgroud)
现在,我必须将其转换为String,但不知道具体如何.
任何Elixir/erlang模块直接执行此操作?
可以检查Elixir中的内存使用情况吗?(也许叫Erlang)
这就是我想要实现的目标:
在更新到我的系统 - MAC之后,我的凤凰应用程序编译得很好但是在我遇到任何路由时都会抛出此错误.
服务器:localhost:4000(http)请求:GET/**(退出)引发异常:**(UndefinedFunctionError)函数:crypto.rand_bytes/1未定义或私有.你是说其中一个:
Run Code Online (Sandbox Code Playgroud)* rand_seed/0 * rand_seed/1 (crypto) :crypto.rand_bytes(20) (plug) lib/plug/request_id.ex:59: Plug.RequestId.generate_request_id/0 (plug) lib/plug/request_id.ex:48: Plug.RequestId.get_request_id/2 (plug) lib/plug/request_id.ex:42: Plug.RequestId.call/2 (olars) lib/olars/endpoint.ex:1: Olars.Endpoint.phoenix_pipeline/1 (olars) lib/plug/debugger.ex:93: Olars.Endpoint."call (overridable 3)"/2 (olars) lib/olars/endpoint.ex:1: Olars.Endpoint.call/2 (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4 (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
我的mix.exs
{:phoenix, "~> 1.2.0"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:phoenix_haml, github: "chrismccord/phoenix_haml"},
{:mariaex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"},
{:arc, "~> 0.5.2"},
{:arc_ecto, "~> 0.3.2"},
{:ex_aws, "~> 0.4.10"},
{:httpoison, "~> 0.9"},
{:poison, "~> …Run Code Online (Sandbox Code Playgroud)