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

  • SEARCH
  • Home
  • 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 6737901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:18:11+00:00 2026-05-26T11:18:11+00:00

My scenario is as follows – I have a client C with function foo()

  • 0

My scenario is as follows –
I have a client C with function foo() which performs some computation.

I’d like a server S, which doesn’t know about foo(), to execute this function instead, and send the result back to the client.

I am trying to determine the best way to perform this in Erlang. I am considering:

  • Hot code swapping – i.e. “upgrade” code in S such that it has the function foo(). Execute and send back to the client.
  • In a distributed manner where nodes are all appropriately registered, do something along the lines of S ! C:foo() – for the purpose of “sending” the function to process/node S

Are there other methods (or features of the language) that I am not thinking of?

Thanks for the help!

  • 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-05-26T11:18:12+00:00Added an answer on May 26, 2026 at 11:18 am

    If the computation function is self contained i.e. does not depend on any other modules or functions on the client C, then what you need to do is a fun (Functional Objects). A fun can be sent across the network and applied by a remote machine and in side the fun, the sender has embedded their address and a way of getting the answer back. So the executor may only see a fun to which they may or may not give an argument, yet inside the fun, the sender has forced a method where by the answer will automatically be sent back. The fun is an abstraction of very many tasks within one thing, and it can be moved around as arguments.
    At the client, you can have code like this:

    
    %% somewhere in the client
    %% client runs on node() == 'client@domain.com'
    
    -module(client).
    -compile(export_all).
    -define(SERVER,{server,'server@domain.com'}).
    
    give_a_server_a_job(Number)-> ?SERVER ! {build_fun(),Number}.
    
    build_fun()->
        FunObject = fun(Param)-> 
                        Answer = Param * 20/1000, %% computation here
                        rpc:call('client@domain.com',client,answer_ready,[Answer])
                    end,
        FunObject.
    
    answer_ready(Answer)-> 
        %%% use Answer for all sorts of funny things....
        io:format("\n\tAnswer is here: ~p~n",[Answer]).
    

    The server then has code like this:

    
    %%% somewhere on the server
    %%% server runs on node() == 'server@domain.com'
    
    -module(server).
    -compile(export_all).
    
    start()-> register(server,spawn(?MODULE,loop,[])).
    
    loop()->
        receive
            {Fun,Arg} -> 
                Fun(Arg),   %% server executes job
                            %% job automatically sends answer back
                            %% to client
                loop();
            stop -> exit(normal);
            _ -> loop()
        end.
    

    In this way, the job executor needs not know about how to send back the reply, The job itself comes knowing how it will send back the answer to however sent the job!. I have used this method of sending functional objects across the network in several project, its so cool !!!

    #### EDIT #####

    If you have a recursive problem, You manipulate recursion using funs. However, you will need at least one library function at the client and/or the server to assist in recursive manipulations. Create a function which should be in the code path of the client as well as the server.

    Another option is to dynamically send code from the server to the client and then using the library: Dynamic Compile erlang to load and execute erlang code at the server from the client. Using dynamic compile, here is an example:

    1> String = "-module(add).\n -export([add/2]). \n add(A,B) -> A + B. \n".
    "-module(add).\n -export([add/2]). \n add(A,B) -> A + B. \n"
    2> dynamic_compile:load_from_string(String).
    {module,add}
    3> add:add(2,5).
    7
    4>
    

    What we see above is a piece of module code that is compiled and loaded dynamically from a string. If the library enabling this is available at the server and client , then each entity can send code as a string and its loaded and executed dynamically at the other. This code can be unloaded after use. Lets look at the Fibonacci function and how it can be sent and executed at the server:

    %% This is the normal Fibonacci code which we are to convert into a string:
    
    -module(fib).
    -export([fib/1]).
    
    fib(N) when N == 0 -> 0;
    fib(N) when (N < 3) and (N > 0) -> 1;
    fib(N) when N > 0 -> fib(N-1) + fib(N-2).
    
    %% In String format, this would now become this piece of code
    StringCode = " -module(fib).\n -export([fib/1]). \nfib(N) when N == 0 -> 0;\n fib(N) when (N < 3) and (N > 0) -> 1;\n fib(N) when N > 0 -> fib(N-1) + fib(N-2). \n".
    
    %% Then the client would send this string above to the server and the server would 
    %% dynamically load the code and execute it

    send_fib_code(Arg)-> {ServerRegName,ServerNode} ! {string,StringCode,fib,Arg}, ok. get_answer({fib,of,This,is,That}) -> io:format("Fibonacci (from server) of ~p is: ~p~n",[This,That]). %%% At Server loop(ServerState)-> receive {string,StringCode,Fib,Arg} when Fib == fib -> try dynamic_compile:load_from_string(StringCode) of {module,AnyMod} -> Answer = AnyMod:fib(Arg), %%% send answer back to client %%% should be asynchronously %%% as the channels are different & not make %% client wait rpc:call('client@domain.com',client,get_answer,[{fib,of,Arg,is,Answer}]) catch _:_ -> error_logger:error_report(["Failed to Dynamic Compile & Load Module from client"]) end, loop(ServerState); _ -> loop(ServerState) end.

    That piece of rough code can show you what am trying to say. However, you remember to unload all un-usable dynamic modules. Also you can a have a way in which the server tries to check wether such a module was loaded already before loading it again. I advise that you donot copy and paste the above code. Look at it and understand it and then write your own version that can do the job.
    success !!!

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

Sidebar

Related Questions

My scenario is as follows: I have a client-server application. The client is deployed
I have an sql scenario as follows which I have been trying to improve.
I have a scenario which is as follows:- A form containing 2 grids. The
My scenario is as follows: I have some objects (Messages) that can be tagged
I have a scenario as follows: I have one Windows 2003 server that has
The scenario is as follows, I have 3 objects (i simplified the names) named
The architecture for this scenario is as follows: I have a table of items
I've confused myself nicely here. My scenario is as follows: function DesignPad() { function
User scenario is like follows. My application has list of companies. Each company has
The scenario is as follows. We have 2 servers; one 'old' running old webshop

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.