Sending message Pid Erlang

天涯浪子 提交于 2019-12-12 16:55:58

问题


I would like to know, how can i send a message to process with erlang. I did start a process and the output shows me that the pid is <0.39.0>. My problem is how can i send a message to this process (<0.39.0>) manually.

Anyhelp would be appreciated


回答1:


While list_to_pid/1 can indeed be used to construct a PID and use it to send messages its usage is discouraged:

This BIF is intended for debugging and for use in the Erlang operating system. It should not be used in application programs.

A better approach would be to save the PID when you start the process:

1> P = spawn(fun() -> receive _ -> ok end end).
<0.34.0>
2> P!hi.
hi



回答2:


(emacs@yus-iMac.local)100> P = list_to_pid("<0.39.0>").
<0.39.0>
(emacs@yus-iMac.local)101> P!aaa.
aaa



回答3:


Besides other solution,REGISTER func maybe helpful.

    register(regpid,spawn(fun() -> receive _ ok end end).
    regpid ! msg.

you can send msg to regpid everywhere.



来源:https://stackoverflow.com/questions/14169931/sending-message-pid-erlang

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