Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:29:44+00:00 2026-06-05T02:29:44+00:00

I have simple erlang module and I want to rewrite it based on OTP

  • 0

I have simple erlang module and I want to rewrite it based on OTP principles. But I can not determine what opt template I should use.

Module’s code:

-module(main).

-export([start/0, loop/0]).

start() -> 
    Mypid = spawn(main, loop, []),
    register( main, Mypid).

loop() ->
    receive
        [Pid, getinfo] -> Pid! [self(), welcome],
            io:fwrite( "Got ~p.~n", [Pid] ),
            // spawn new process here
            loop();
        quit -> ok;
        X ->
            io:fwrite( "Got ~p.~n", [ X ] ),
            // spawn new process here
            loop()
    end.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:29:45+00:00Added an answer on June 5, 2026 at 2:29 am

    gen_server would be fine.

    Couple things:

    • it is a bad practice to send message to yourself
    • messages are usually tuples not lists because they are not dynamic
    • despite your comment, you do not spawn the new process.
      Call to loop/0 enters the same loop.

    Gen_server init would hold your start/0 body. API calls sequence and proxy your calls via gen_server to handle_calls. To spawn new process on function call, add spawn function to the body of desired handle_call. Do not use handle_info to handle incoming messages — instead of sending them call the gen_server API and ‘translate’ your call into gen_server:call or cast. e.g.

    start_link() ->
        gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
    
    init(_) ->
        {ok, #state{}}
    
    welcome(Arg) ->
       gen_server:cast(?MODULE, {welcome, Arg}).
    
    handle_cast({welcome, Arg},_,State) ->
      io:format("gen_server PID: ~p~n", [self()]),
      spawn(?MODULE, some_fun, [Arg]),
      {noreply, State}
    
    some_fun(Arg) ->
      io:format("Incoming arguments ~p to me: ~p~n",[Arg, self()]).
    

    I have never compiled above, but it should give you the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the erlang TCP/IP library have some limitations? I've done some searching but can't
CouchDB, version 0.10.0, using native erlang views. I have a simple document of the
I have a simple server: -module(simple_server). -export([loop/0]). loop() -> receive {fact, N, Sender} ->
I have written a simple erlang app using gen_server. When starting it with application:start(myapp),
I have simple script, I want to get my images and then divide them
I have simple problem as I am not much familiar with Java GUI. I
I have simple WCF Service Application (based on this tutorial : Getting Started ).
i have simple table with two fields: ID and Date how can i do
I have simple one column HTML files ( ebooks from Gutenberg Project). I want
Can someone help me with this simple distributed erlang exampe. How can i run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.