问题描述
我正在分散Erlang应用程序。我目前正在使用单个PC并通过使用 -sname
标志初始化 erl
来创建多个节点。 当我的家庭节点使用 spawn / 4
生成一个进程时,我可以看到通过调用生成的输出 io:format / 2
在其家中的 erl
实例。
当我使用 spawn / 4
与 register_name
结合使用远程生成进程时, io:format / 2 有时被重定向到 erl
实例,其中远程 spawn / 4
调用,有时保持完全不可见。
同样,当我使用 rpc:call / 4
,输出 io:format / 2
调用被重定向回 erl
实例,其中`rpc:call / 4'调用。
如何让进程将调试输出发回到其父 erl
?
可以使用第一个节点的erlang:group_leader()的结果,在第二个节点上为io:format / 3提供第一个参数。
从第一个节点开始,在全局注册本地shell进程组长:
erl -sname a
一个@本地)1> global:register_name(global_io_srv,group_leader())。
是
启动第二个节点,连接,使用全局注册的进程作为io设备
erl -sname b
(b @ localhost)1> net_kernel:连接(一@本地)。
true
(b @ localhost)2> io:format(global:whereis_name(global_io_srv),test output,[])。
ok
您将在第一个节点中看到测试输出。
这与Christian建议的一样,只是稍微明确一些。所以你可以使用error_logger来生产日志记录,io:format / 3只是为了快速调试。
I am working on a decentralized Erlang application. I am currently working on a single PC and creating multiple nodes by initializing erl
with the -sname
flag.
When I spawn a process using spawn/4
on its home node, I can see output generated by calls io:format/2
within that process in its home erl
instance.
When I spawn a process remotely by using spawn/4
in combination with register_name
, output of io:format/2
is sometimes redirected back to the erl
instance where the remote spawn/4
call was made, and sometimes remains completely invisible.
Similarly, when I use rpc:call/4
, output of io:format/2
calls is redirected back to the erl
instance where the `rpc:call/4' call is made.
How do you get a process to emit debugging output back to its parent erl
instance?
You can supply 1st argument to io:format/3 on the second node, using result of erlang:group_leader() from the first node.
Starting first node, registering local shell process group leader globally:
erl -sname a
(a@localhost)1> global:register_name(global_io_srv, group_leader()).
yes
Starting second node, connecting, using globally registered process as io device
erl -sname b
(b@localhost)1> net_kernel:connect(a@localhost).
true
(b@localhost)2> io:format(global:whereis_name(global_io_srv),"test output",[]).
ok
You will see test output in the first node.This is the same way that Christian suggested, just a bit more explicit. So you can have error_logger for production logging and io:format/3 just for quick debugging.
这篇关于Erlang:如何在远程节点上生成的进程中查看io:format / 2调用的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!