[erlang-questions] [Code Review] Is this a good idea? I linking 3 process manually (not using recursive)

I Gusti Ngurah Oka Prinarjaya okaprinarjaya@REDACTED
Sun Mar 10 11:11:58 CET 2019


Hi Bengt,

Thank you for your review :)

I don't quite understand with some of your thought. Then i hope you not
mind to give more explanation.

>> Seasoned Erlang programmers will tell you this is not the OTP way, or a
waste of modules
Did you mean my code here: https://pastebin.com/ECf3jhZ2 wasn't the OTP
way?
I learn from here: https://learnyousomeerlang.com/errors-and-processes and
i do really a beginner at programming with erlang.

>> So only do this until you have a better understanding.
What is that? Did you mean *doing one module with the interface functions
to a gen_server and one module for the gen_server callbacks.?*
or what?

>> Conflating the function (or even more often, module) and the process is
a common mistake in the beginning. When my fellow programmers are
experienced, but not used >> to process oriented programming.
 Any tutorials / reference for me to avoid this mistake ?

Thank you :)








Pada tanggal Min, 10 Mar 2019 pukul 14.49 bengt <cean.ebengt@REDACTED>
menulis:

> Conflating the function (or even more often, module) and the process is a
> common mistake in the beginning. When my fellow programmers are
> experienced, but not used to process oriented programming, I have found
> that it helps to put the code that is run in different process in different
> modules. Ex: one module with the interface functions to a gen_server and
> one module for the gen_server callbacks. Seasoned Erlang programmers will
> tell you this is not the OTP way, or a waste of modules. So only do this
> until you have a better understanding.
>
> bengt
>
> On 10 Mar 2019, at 04:57, I Gusti Ngurah Oka Prinarjaya <
> okaprinarjaya@REDACTED> wrote:
>
> Hi Bengt,
>
> Thank you for the idea,
>
> I've implement your idea, from performance side, i think using two version
> of function will have better performance because there's no need to
> checking using IF statement.
>
> I worry at first to implement this idea, because i think different version
> of function will have different PID, hahaha it turns out exactly have same
> PID.
> this is  my new code https://pastebin.com/ECf3jhZ2 . Please review if
> there's any improvement needs.
>
> Thank you
>
>
>
>
>
>
>
> Pada tanggal Min, 10 Mar 2019 pukul 05.23 bengt <cean.ebengt@REDACTED>
> menulis:
>
>> Greetings,
>>
>> The error happens the second time any of the three processes call
>> erlang:register/2. They already registered themselves the first time. If
>> you want this structure, but not the error, you have to do the register
>> before starting the loop. Eg, split the functions into two. One that do the
>> register and then calls the second that does the loop.
>>
>> bengt
>>
>> On 9 Mar 2019, at 14:41, I Gusti Ngurah Oka Prinarjaya <
>> okaprinarjaya@REDACTED> wrote:
>>
>> Hi Attila Rajmund Nohl,
>>
>> After i implement your suggestion, i get error when execute any of
>> function call_the_*_p()
>> for example:
>>
>> 3> newbie:starter().
>> <0.112.0>
>> 4> newbie:call_the_second().
>> =ERROR REPORT==== 9-Mar-2019::20:35:15.069388 ===
>> Error in process <0.113.0> with exit value:
>> {badarg,[{erlang,register,[pidsecondp,<0.113.0>],[]},
>>          {newbie,the_second_p,0,
>>
>> [{file,"/Users/okaprinarjaya/Oprek/Erlang-Oprek/oprek-lagi/src/newbie.erl"},
>>                   {line,17}]}]}
>>
>> {<0.113.0>,"the_second_p()","Halo second p!"}
>>
>> This is my new modified code https://pastebin.com/iYCxkkuz
>>
>>
>> Pada tanggal Jum, 8 Mar 2019 pukul 21.03 Attila Rajmund Nohl <
>> attila.r.nohl@REDACTED> menulis:
>>
>>> I Gusti Ngurah Oka Prinarjaya <okaprinarjaya@REDACTED> ezt írta
>>> (időpont: 2019. márc. 8., P, 12:11):
>>> >
>>> > Hi Folks,
>>> >
>>> > I need your help to review my code. I create and linking 3 process
>>> manually without using recursive. And inside p1 and p2 i using IF statement
>>> to check to make sure spawning process will only once.
>>> >
>>> > I mean, is part code below is a good idea?
>>> >
>>> > IsPidExists = whereis(xxx),
>>> >   if IsPidExists =:= undefined ->
>>> >     Pid = spawn_link(?MODULE, the_p, []),
>>> >     register(xxx, Pid);
>>> >     true -> true
>>> >   end,
>>>
>>> Generally this is not a good idea, there's a race condition between
>>> checking that the process is registered (the whereis/1 call) and
>>> registering the new process. A better idea is to start the process and
>>> let the process itself to register. If register fails, it means that
>>> there's already a process registered, so the just started process can
>>> terminate. So your code could look like something like this:
>>>
>>> starter() ->
>>>   spawn(?MODULE, the_first_p, []).
>>>
>>> the_first_p() ->
>>>   register(pidfirstp, self()),
>>>   spawn_link(?MODULE, the_second_p, []),
>>>   ...
>>>   the_first_p().
>>>
>>> Actually if the register call fails, it throws a badarg and the
>>> process dies automatically, simplifying the code.
>>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190310/e546b210/attachment.htm>


More information about the erlang-questions mailing list