监听进程 Erlang

Listening To Process Erlang

我正在尝试根据 synrc/fs library 检测到的文件夹更改执行操作。每次 fs 捕获更改并执行操作时,我都想收到此通知,例如打印更改后的文件名。

我试过下面的代码,但只执行了第一次!

say_hello() ->
    fs:start_link(fs_watcher, "/Users/foldername"),
    fs:subscribe(fs_watcher),
    receive
        {Watcher_process, {Fs, File_event}, {ChangedFile, Type}} ->
            io:format("~p was ~p ~n",[ChangedFile,File_event])
    end.

如有可能,请提供任何有用的帮助以及 link 和说明!谢谢:)

如果您希望函数继续接收相同类型的消息,您可以使用递归:

say_hello() ->
  fs:start_link(fs_watcher, "/Users/foldername"),
  fs:subscribe(fs_watcher),
  recur().

recur()->
  receive
     {Watcher_process, {Fs, File_event}, {ChangedFile, Type}} ->
         io:format("~p was ~p ~n",[ChangedFile,File_event]),
         recur()
  end.

然后您将不得不考虑一种方法来完成该功能。

你需要递归调用receive:

say_hello() ->
    fs:start_link(fs_watcher, "/Users/foldername"),
    fs:subscribe(fs_watcher),
    loop().

loop() ->
    receive
        {Watcher_process, {Fs, File_event}, {ChangedFile, Type}} ->
            io:format("~p was ~p ~n",[ChangedFile,File_event]),
            loop()
    end.