Erlang: How to view output of io:format/2 calls in processes spawned on remote nodes

牧云@^-^@ 提交于 2019-12-04 13:24:28

问题


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?


回答1:


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.




回答2:


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?




回答3:


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>


来源:https://stackoverflow.com/questions/2587185/erlang-how-to-view-output-of-ioformat-2-calls-in-processes-spawned-on-remote-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!