user156996
user156996

Reputation: 33

How do I get the process id of the calling process in erlang?

A took a look at http://erlang.org/doc/apps/inets/http_client.html and found the following:

An ordinary asynchronous request. The result will be sent to the calling process on the form {http, {ReqestId, Result}}

      5 > {ok, RequestId} =
      http:request(get, {"http://www.erlang.org", []}, [], [{sync, false}]).
In this case the calling process is the shell, so we receive the result.

      6 >  receive {http, {RequestId, Result}} -> ok after 500 -> error end.
      ok

http:request passes a message to me after calling it that way, but how does it know my process id? As far as I understand, http:request has to do "Pid ! {http, {RequestId, Result}" to send the result to me, but where does it know the value of Pid?

Upvotes: 3

Views: 6028

Answers (2)

Zed
Zed

Reputation: 57658

If you look at the definition of the #request{} record (in httpc_internal.hrl), you will see that there is a field called from. It contains the caller's pid; that's how the server will be able to send a message to the caller later.

Looking at the source code of http module you will see that your call will eventually reach the handle_request function, where the from field is set to self().

Upvotes: 6

Steve Gilham
Steve Gilham

Reputation: 11277

You don't -- the calling process has to supply its Pid if it wants a reply. Do something like

Server ! {self(), arguments}

and then the first element of the tuple is the return address.

Upvotes: 2

Related Questions