Erlang:如何查看远程节点上生成的进程中 io:format/2 调用的输出

发布于 2024-08-28 06:03:37 字数 504 浏览 4 评论 0原文

我正在开发一个去中心化的 Erlang 应用程序。我目前正在一台 PC 上工作,并通过使用 -sname 标志初始化 erl 来创建多个节点。

当我在其主节点上使用 spawn/4 生成一个进程时,我可以在其主 erl 中看到该进程内调用 io:format/2 生成的输出实例。

当我结合使用 spawn/4register_name 远程生成进程时,io:format/2 的输出有时会重定向回进行远程 spawn/4 调用的 erl 实例,有时保持完全不可见。

同样,当我使用 rpc:call/4 时,io:format/2 调用的输出将重定向回 erl 实例,其中 ` rpc:call/4' 调用已进行。

如何让进程将调试输出发送回其父 erl 实例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

善良天后 2024-09-04 06:03:37

您可以使用第一个节点的 erlang:group_leader() 的结果,向第二个节点上的 io:format/3 提供第一个参数。

启动第一个节点,全局注册本地 shell 进程组领导者:

erl -sname a
(a@localhost)1> global:register_name(global_io_srv, group_leader()).
yes

启动第二个节点,连接,使用全局注册进程作为 io 设备

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

您将在第一个节点中看到测试输出。
这与克里斯蒂安建议的方式相同,只是更明确一点。因此,您可以使用 error_logger 进行生产日志记录,使用 io:format/3 进行快速调试。

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.

初熏 2024-09-04 06:03:37

您所看到的是进程的组领导者设置为它们产生的节点上的 pid。请参阅 erlang:group_leader。组长是他们将输出发送到的地方。

您将此输出称为“调试输出”,因此您确定不想在节点上启动 sasl 应用程序并使用 error_logger

What you are seeing is processes with their group leader set to a pid on the node they were spawned from. See erlang:group_leader. The group leader is where they send their output to.

You call this output "debugging output", so are you sure that you dont want to start the sasl application on the nodes and use error_logger?

演多会厌 2024-09-04 06:03:37

请参阅我的答案问题 Erlang:RPC 到节点并在该节点上输出节点了解如何实现输出到不同地方的一些细节。没有提到的是,即使在运行 shell 中也可以运行远程 shell。只需按 Ctrl+G(启动时提示 ^G),然后在 h 下即可获得帮助(按 h然后输入Enter)。

示例:假设您已通过 erl -sname foo 运行 erlang 节点。比:

$ erl -sname bar
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5  (abort with ^G)
(bar@hynek-notebook)1>
User switch command
 --> r 'foo@hynek-notebook'
 --> j
   1  {shell,start,[init]}
   2* {'foo@hynek-notebook',shell,start,[]}
 --> h
  c [nn]            - connect to job
  i [nn]            - interrupt job
  k [nn]            - kill job
  j                 - list all jobs
  s [shell]         - start local shell
  r [node [shell]]  - start remote shell
  q        - quit erlang
  ? | h             - this message
 --> c
Eshell V5.7.5  (abort with ^G)
(foo@hynek-notebook)1>

See mine answer to question Erlang : RPC to a node with output on that node for some details how to achieve output to different places. Which is not mentioned, you can run remote shell even in running shell. Just press Ctrl+G (^G hint on startup) and than you have help under h (press h and than Enter).

Example: Assume you have running erlang node by erl -sname foo. Than:

$ erl -sname bar
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5  (abort with ^G)
(bar@hynek-notebook)1>
User switch command
 --> r 'foo@hynek-notebook'
 --> j
   1  {shell,start,[init]}
   2* {'foo@hynek-notebook',shell,start,[]}
 --> h
  c [nn]            - connect to job
  i [nn]            - interrupt job
  k [nn]            - kill job
  j                 - list all jobs
  s [shell]         - start local shell
  r [node [shell]]  - start remote shell
  q        - quit erlang
  ? | h             - this message
 --> c
Eshell V5.7.5  (abort with ^G)
(foo@hynek-notebook)1>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文