scof007
scof007

Reputation: 443

How to manually send a message to a process which I just spawned?

How can I send a message to process with Erlang? I did start a process and the output shows me that the pid (process identifier) is <0.39.0>. My question is how can I send a message to this process (<0.39.0>) manually.

Upvotes: 3

Views: 3599

Answers (3)

liuyang1
liuyang1

Reputation: 1725

Besides other solution,REGISTER func maybe helpful.

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

you can send msg to regpid everywhere.

Upvotes: 2

thanos
thanos

Reputation: 5858

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

Upvotes: 6

Chen Yu
Chen Yu

Reputation: 4077

([email protected])100> P = list_to_pid("<0.39.0>").
<0.39.0>
([email protected])101> P!aaa.
aaa

Upvotes: 3

Related Questions