从其他进程失败的任务中接受套接字
Accepted socket from task failing in other process
我正在尝试测试一些长生不老药代码,遇到了一些我不明白的 gen_tcp 行为。当我 gen_tcp.accept 一个套接字时,我可能 "access" 它在我创建它的任务中,但不是另一个。我认为这是一个 "controlling_process" 问题,但即使我添加了任何使用 :inet.getstat 的尝试都会导致无效参数错误。我也无法在套接字上进行接收,Elixir 声称它已关闭,但对于这个问题,getstat 更容易。见下文:
defmodule ElixirQuestion do
def serve_one_client(socket, pid)
do
{:ok, server_socket} = :gen_tcp.accept(socket)
:ok = :gen_tcp.controlling_process(server_socket, pid)
IO.inspect(:inet.getstat(server_socket))
server_socket
end
end
{:ok, socket} = :gen_tcp.listen(0, [:binary,
{:packet, :raw},
{:active, false}])
{:ok, port_number} = :inet.port(socket)
server_task = Task.async(fn -> ElixirQuestion.serve_one_client(socket, self()) end)
{:ok, _client_socket} = :gen_tcp.connect('localhost', port_number, [active: false])
server_socket = Task.await(server_task)
IO.inspect(:inet.getstat(server_socket))
预期输出
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
收到输出
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
{:error, :einval}
Elixir 版本
Erlang/OTP 19 [erts-8.3.5.3] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
Elixir 1.4.5
Task.async
中的 self()
将 return Task
进程的 PID。由于这里要的是父进程的值,所以需要把这个值保存在传递给Task.async
的fn外面,然后在里面使用
变化中:
server_task = Task.async(fn -> ElixirQuestion.serve_one_client(socket, self()) end)
至:
me = self()
server_task = Task.async(fn -> ElixirQuestion.serve_one_client(socket, me) end)
给我你期望的输出:
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
我正在尝试测试一些长生不老药代码,遇到了一些我不明白的 gen_tcp 行为。当我 gen_tcp.accept 一个套接字时,我可能 "access" 它在我创建它的任务中,但不是另一个。我认为这是一个 "controlling_process" 问题,但即使我添加了任何使用 :inet.getstat 的尝试都会导致无效参数错误。我也无法在套接字上进行接收,Elixir 声称它已关闭,但对于这个问题,getstat 更容易。见下文:
defmodule ElixirQuestion do
def serve_one_client(socket, pid)
do
{:ok, server_socket} = :gen_tcp.accept(socket)
:ok = :gen_tcp.controlling_process(server_socket, pid)
IO.inspect(:inet.getstat(server_socket))
server_socket
end
end
{:ok, socket} = :gen_tcp.listen(0, [:binary,
{:packet, :raw},
{:active, false}])
{:ok, port_number} = :inet.port(socket)
server_task = Task.async(fn -> ElixirQuestion.serve_one_client(socket, self()) end)
{:ok, _client_socket} = :gen_tcp.connect('localhost', port_number, [active: false])
server_socket = Task.await(server_task)
IO.inspect(:inet.getstat(server_socket))
预期输出
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
收到输出
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
{:error, :einval}
Elixir 版本
Erlang/OTP 19 [erts-8.3.5.3] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
Elixir 1.4.5
Task.async
中的 self()
将 return Task
进程的 PID。由于这里要的是父进程的值,所以需要把这个值保存在传递给Task.async
的fn外面,然后在里面使用
变化中:
server_task = Task.async(fn -> ElixirQuestion.serve_one_client(socket, self()) end)
至:
me = self()
server_task = Task.async(fn -> ElixirQuestion.serve_one_client(socket, me) end)
给我你期望的输出:
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}
{:ok,
[recv_oct: 0, recv_cnt: 0, recv_max: 0, recv_avg: 0, recv_dvi: 0, send_oct: 0,
send_cnt: 0, send_max: 0, send_avg: 0, send_pend: 0]}