Я только что начал читать книгу «Программирование Эрланг». Существует очень один пример, который работает только с файлом.Выполнение процессов связи
Если я выполняю:
> 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).
Client
%% ---
%% 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.