[erlang-questions] Noob question: Need some help to limit process spawing

avinash D'silva evnix.com@REDACTED
Sun Aug 16 18:27:28 CEST 2015


@Garret,

I purposely changed the variable names, so that they would be easier to
understand,
as I did not want to confuse/make it hard for the readers with the names
specific to the project.

I solved the problem by using a *mutex*:
but I am not sure if this is the right way to do it in Erlang or if there
are better ways of doing the same?

myfunction(Value)->

Val2=iolist_to_binary([Value,"_P"]),

case pg2:get_members(Val2) of

        [] ->

            case ets:lookup(myset,mylock) of

           [{_,0}] ->

                   ets:insert(myset, {mylock, 1}),
            {ok,P}=mymodule:start_link(),
            pg2:join(Val2,P),
            ets:insert(myset, {mylock, 0}),
            gen_server:call(P,{push,Msg});

           [{_,1}] ->

                   %try again
                   mymodule:myfunction(Value)
        end;

        Otherwise ->

            [Px|_]=Otherwise,
            gen_server:call(Px,{push,Msg})
end.

On Sun, Aug 16, 2015 at 9:36 PM, Garrett Smith <g@REDACTED> wrote:

> On Sun, Aug 16, 2015 at 8:37 AM, Edward Halls <ehalls@REDACTED> wrote:
> > Perhaps some better syntax highlighting can help you spot this in the
> > future.
> >
> > A good ide will also tell you when there are unused variables.
>
> Better programming conventions - in particular making the code say
> what's going on - is even better than a super intelligent IDE that
> tells you what's wrong as you're typing. I turn those helper features
> off. Or, I would if Emacs had them on by default.
>
> > On 16 Aug 2015 2:22 pm, "Robert Raschke" <rtrlists@REDACTED>
> wrote:
> >>
> >> Could be the typo? Value2 vs Val2.
> >>
> >> On Aug 16, 2015 12:44 PM, "avinash D'silva" <evnix.com@REDACTED>
> wrote:
> >>>
> >>> Hi,
> >>>
> >>> I have these lines code:
> >>>
> >>>  timer:apply_after(5*1000,mymodule,myfunction,[Value]),
> >>>  timer:apply_after(5*1000,mymodule,myfunction,[Value]),
> >>>  timer:apply_after(5*1000,mymodule,myfunction,[Value]),
> >>>  timer:apply_after(5*1000,mymodule,myfunction,[Value]),
> >>>  timer:apply_after(5*1000,mymodule,myfunction,[Value]),
> >>>
> >>> "myfunction" looks like this:
> >>> myfunction(Value)->
>
> This should be a function:
>
> >>> Val2=iolist_to_binary([Value,"_P"]),
>
> You're making a decision to add this suffix here? Why? A well named
> function will make that obvious.
>
> Any time you find yourself adding 2, 3, 4s to the end of a variable,
> stop. What's the actual name of the variable there? I mean, what is
> it? Use that.
>
> Oh, and "value" should be "term" here. Kidding of course - it's no
> doubt a value, but take advantage of the variable name. Just spend a
> moment and think about what that thing actually is in the context of
> your function/problem.
>
> I'd flatten this all out:
>
> >>> case pg2:get_members(Value2) of
> >>>
> >>>         [] ->
> >>>             {ok,P}=mymodule:start_link(),
> >>>             pg2:join(Value2,P2),
> >>>             gen_server:call(P2,{push,Msg});
> >>>         Otherwise ->
> >>>             [Px|_]=Otherwise,
> >>>             gen_server:call(Px,{push,Msg})
> >>>       end,
>
> to be something like:
>
>    handle_members(members_for_xxx(XXX))
>
> I don't know what XXX is here (that's a bad name too) but it should make
> sense.
>
> >>> the problem is instead of creating a single process of mymodule.
> >>> there are 5 processes, as time taken to register the process is more,
>
> I didn't answer your question as I fixate on code clarity and it's an
> emotional blocker for me :)
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



-- 
Powered By codologic <http://codologic.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150816/bf9694ca/attachment.htm>


More information about the erlang-questions mailing list