执行过程通信
Performing processes communication
我刚刚开始阅读 Programming Erlang 这本书。有一个非常单一的例子只适用于文件。
如果我执行:
> c(afile_server).
> c(afile_client).
> Server = afile_server:start(".").
> client:get_file(Server, "file1").
> client:get_file(Server, "file2").
> client:get_file(Server, "file3").
一切正常,只是文件。显示三个文件内容。
但如果我这样做:
> c(afile_server).
> Server = afile_server:start(".").
> Server ! { self(), { get_file, "file1" } }.
> receive
{ Server, Content }
Content
end.
它只适用于文件。
但是如果我更改了我试图读取的文件(在第一次调用之后),例如
> Server ! { self(), { get_file, "file2" } }.
> receive
{ Server, Content }
Content
end.
接收块,没有任何返回。
你们能帮忙吗?我想,这是一个非常愚蠢的错误!
问候。
文件:
服务器:
-module(afile_server).
-export([start/1, loop/1]).
start(Dir) -> spawn(afile_server, loop, [Dir]).
loop(Dir) ->
receive
{Client, list_dir} ->
Client ! {self(), file:list_dir(Dir)};
{Client, {get_file, File}} ->
Full = filename:join(Dir, File),
Client ! {self(), file:read_file(Full)}
end,
loop(Dir).
客户端
%% ---
%% Excerpted from "Programming Erlang, Second Edition",
%% published by The Pragmatic Bookshelf.
%% Copyrights apply to this code. It may not be used to create training material,
%% courses, books, articles, and the like. Contact us if you are in doubt.
%% We make no guarantees that this code is fit for any purpose.
%% Visit http://www.pragmaticprogrammer.com/titles/jaerlang2 for more book information.
%%---
-module(afile_client).
-export([ls/1, get_file/2]).
ls(Server) ->
Server ! {self(), list_dir},
receive
{Server, FileList} ->
FileList
end.
get_file(Server, File) ->
Server ! {self(), {get_file, File}},
receive
{Server, Content} ->
Content
end.
那是因为您在 shell 中 运行 并在 receive
模式中重用名称 Content
并且这 2 个文件可能具有不同的内容你的文件系统,所以 receive
阻塞,直到进程收到一条内容相同的消息。要解决此问题,您可以每次为 Content
使用不同的名称,或者使用 f(Content)
.
显式 "forget" 变量
1> c(afile_server).
{ok,afile_server}
2> Server = afile_server:start(".").
<0.64.0>
3> Server ! { self(), { get_file, "file1" } }.
{<0.57.0>,{get_file,"file1"}}
4> receive {Server, Content} -> Content end.
{ok,<<"file1\n">>}
5> Server ! { self(), { get_file, "file2" } }.
{<0.57.0>,{get_file,"file2"}}
6> receive {Server, Content} -> Content after 1000 -> timeout end.
timeout
7> receive {Server, Content2} -> Content2 end.
{ok,<<"file2\n">>}
8> Server ! { self(), { get_file, "file3" } }.
{<0.57.0>,{get_file,"file3"}}
9> receive {Server, Content} -> Content after 1000 -> timeout end.
timeout
10> f(Content).
ok
11> receive {Server, Content} -> Content after 1000 -> timeout end.
{ok,<<"file3\n">>}
我刚刚开始阅读 Programming Erlang 这本书。有一个非常单一的例子只适用于文件。
如果我执行:
> c(afile_server).
> c(afile_client).
> Server = afile_server:start(".").
> client:get_file(Server, "file1").
> client:get_file(Server, "file2").
> client:get_file(Server, "file3").
一切正常,只是文件。显示三个文件内容。
但如果我这样做:
> c(afile_server).
> Server = afile_server:start(".").
> Server ! { self(), { get_file, "file1" } }.
> receive
{ Server, Content }
Content
end.
它只适用于文件。 但是如果我更改了我试图读取的文件(在第一次调用之后),例如
> Server ! { self(), { get_file, "file2" } }.
> receive
{ Server, Content }
Content
end.
接收块,没有任何返回。 你们能帮忙吗?我想,这是一个非常愚蠢的错误! 问候。
文件:
服务器:
-module(afile_server).
-export([start/1, loop/1]).
start(Dir) -> spawn(afile_server, loop, [Dir]).
loop(Dir) ->
receive
{Client, list_dir} ->
Client ! {self(), file:list_dir(Dir)};
{Client, {get_file, File}} ->
Full = filename:join(Dir, File),
Client ! {self(), file:read_file(Full)}
end,
loop(Dir).
客户端
%% ---
%% Excerpted from "Programming Erlang, Second Edition",
%% published by The Pragmatic Bookshelf.
%% Copyrights apply to this code. It may not be used to create training material,
%% courses, books, articles, and the like. Contact us if you are in doubt.
%% We make no guarantees that this code is fit for any purpose.
%% Visit http://www.pragmaticprogrammer.com/titles/jaerlang2 for more book information.
%%---
-module(afile_client).
-export([ls/1, get_file/2]).
ls(Server) ->
Server ! {self(), list_dir},
receive
{Server, FileList} ->
FileList
end.
get_file(Server, File) ->
Server ! {self(), {get_file, File}},
receive
{Server, Content} ->
Content
end.
那是因为您在 shell 中 运行 并在 receive
模式中重用名称 Content
并且这 2 个文件可能具有不同的内容你的文件系统,所以 receive
阻塞,直到进程收到一条内容相同的消息。要解决此问题,您可以每次为 Content
使用不同的名称,或者使用 f(Content)
.
1> c(afile_server).
{ok,afile_server}
2> Server = afile_server:start(".").
<0.64.0>
3> Server ! { self(), { get_file, "file1" } }.
{<0.57.0>,{get_file,"file1"}}
4> receive {Server, Content} -> Content end.
{ok,<<"file1\n">>}
5> Server ! { self(), { get_file, "file2" } }.
{<0.57.0>,{get_file,"file2"}}
6> receive {Server, Content} -> Content after 1000 -> timeout end.
timeout
7> receive {Server, Content2} -> Content2 end.
{ok,<<"file2\n">>}
8> Server ! { self(), { get_file, "file3" } }.
{<0.57.0>,{get_file,"file3"}}
9> receive {Server, Content} -> Content after 1000 -> timeout end.
timeout
10> f(Content).
ok
11> receive {Server, Content} -> Content after 1000 -> timeout end.
{ok,<<"file3\n">>}