Comments on: An Introduction to gen_server: “ErlyBank” http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/ Linking You to Erlang Sun, 22 May 2011 00:00:34 +0000 http://wordpress.org/?v=2.6.1 By: pavlov http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-1421 pavlov Thu, 08 Jan 2009 00:22:57 +0000 http://spawnlink.com/?p=27#comment-1421 How would I create an HTTP interface to handle_call? How would I create an HTTP interface to handle_call?

]]>
By: John Bender http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-1049 John Bender Sun, 14 Dec 2008 22:02:23 +0000 http://spawnlink.com/?p=27#comment-1049 Awesome set of articles. They've been linked to on http://erlanguid.com, and I hope there are other avenues for people to find them. Great read, and thank you for taking the time to teach others! Awesome set of articles. They’ve been linked to on http://erlanguid.com, and I hope there are other avenues for people to find them. Great read, and thank you for taking the time to teach others!

]]>
By: David Weldon http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-145 David Weldon Tue, 30 Sep 2008 02:29:13 +0000 http://spawnlink.com/?p=27#comment-145 That makes perfect sense. Thanks for the fast reply Mitchell. Your articles are fantastic! That makes perfect sense. Thanks for the fast reply Mitchell. Your articles are fantastic!

]]>
By: Mitchell http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-141 Mitchell Mon, 29 Sep 2008 23:19:45 +0000 http://spawnlink.com/?p=27#comment-141 David, Literally, I was correct in saying that a gen_server can't handle multiple clients <em>concurrently</em>. But normally this isn't an issue since casting is so quick (since its a one-way message). If you want a gen_server to begin processing a message and be able to send back to the caller, I would spawn off the long running process, store the <span class="code inline">From</span> parameter of <tt>gen_server:call</tt> and manually use <tt>gen_server:reply</tt> to reply via the spawned process. To put this into a clearer example (I hope), imagine having a math server (based on gen_server) and there is a call which calculates all the primes up to <tt>n</tt>. For certain large n, this process can take quite awhile, so instead of doing all the calculations in the math server process, I would do something like the following: <div class="code"><pre> handle_call({primes, N}, From, State) -> spawn(?MODULE, calculate_primes, [From, N]), {noreply, State}. calculate_primes(From, N) -> Primes = [], % Use some method of calculating prime numbers... gen_server:reply(From, {primes, Primes}). </pre></div>   Hopefully the example above poses a fairly obvious solution. As you can see in handle_call, since it spawns off the calculation work, it is able to become ready to handle more calls/casts/etc. right away. Then, when the calculate_primes method ends, it explicitly sends the respond back to the caller. Mitchell David,

Literally, I was correct in saying that a gen_server can’t handle multiple clients concurrently. But normally this isn’t an issue since casting is so quick (since its a one-way message). If you want a gen_server to begin processing a message and be able to send back to the caller, I would spawn off the long running process, store the From parameter of gen_server:call and manually use gen_server:reply to reply via the spawned process.

To put this into a clearer example (I hope), imagine having a math server (based on gen_server) and there is a call which calculates all the primes up to n. For certain large n, this process can take quite awhile, so instead of doing all the calculations in the math server process, I would do something like the following:

handle_call({primes, N}, From, State) ->
  spawn(?MODULE, calculate_primes, [From, N]),
  {noreply, State}.

calculate_primes(From, N) ->
  Primes = [],
  % Use some method of calculating prime numbers...
  gen_server:reply(From, {primes, Primes}).

 

Hopefully the example above poses a fairly obvious solution. As you can see in handle_call, since it spawns off the calculation work, it is able to become ready to handle more calls/casts/etc. right away. Then, when the calculate_primes method ends, it explicitly sends the respond back to the caller.

Mitchell

]]>
By: David Weldon http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-135 David Weldon Mon, 29 Sep 2008 20:19:41 +0000 http://spawnlink.com/?p=27#comment-135 "A gen_server by itself doesn’t handle multiple clients concurrently. And although this feature would not be much harder to add..." Can you give some brief insight as to how one could accomplish this? “A gen_server by itself doesn’t handle multiple clients concurrently. And although this feature would not be much harder to add…” Can you give some brief insight as to how one could accomplish this?

]]>
By: Performing Real-time Upgrades to an OTP System | spawn_link http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-110 Performing Real-time Upgrades to an OTP System | spawn_link Wed, 24 Sep 2008 17:19:19 +0000 http://spawnlink.com/?p=27#comment-110 [...] final article of the Erlang/OTP introduction series. If you haven’t already, I recommend you read the first article which lays the foundation for the application which we’ll be upgrading in addition to [...] [...] final article of the Erlang/OTP introduction series. If you haven’t already, I recommend you read the first article which lays the foundation for the application which we’ll be upgrading in addition to [...]

]]>
By: An Introduction to Releases with Erlybank | spawn_link http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-92 An Introduction to Releases with Erlybank | spawn_link Wed, 17 Sep 2008 15:05:22 +0000 http://spawnlink.com/?p=27#comment-92 [...] is the sixth article in the otp introduction series. If you haven’t yet, I recommend you start with the first article which talks about gen_server and lays the foundation for our bank system. If you’re a quick [...] [...] is the sixth article in the otp introduction series. If you haven’t yet, I recommend you start with the first article which talks about gen_server and lays the foundation for our bank system. If you’re a quick [...]

]]>
By: Dew Drop - September 17, 2008 | Alvin Ashcraft's Morning Dew http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-90 Dew Drop - September 17, 2008 | Alvin Ashcraft's Morning Dew Wed, 17 Sep 2008 13:37:37 +0000 http://spawnlink.com/?p=27#comment-90 [...] An Introduction to gen_server: "ErlyBank" [...] [...] An Introduction to gen_server: "ErlyBank" [...]

]]>
By: Bundling ErlyBank as an Application | spawn_link http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-81 Bundling ErlyBank as an Application | spawn_link Mon, 15 Sep 2008 15:38:45 +0000 http://spawnlink.com/?p=27#comment-81 [...] is the fifth article in the otp introduction series. If you haven’t yet, I recommend you start with the first article which talks about gen_server and lays the foundation for our bank system. If you’re a quick [...] [...] is the fifth article in the otp introduction series. If you haven’t yet, I recommend you start with the first article which talks about gen_server and lays the foundation for our bank system. If you’re a quick [...]

]]>
By: Using Supervisors to Keep ErlyBank Afloat | spawn_link http://spawnlink.com/articles/an-introduction-to-gen_server-erlybank/#comment-61 Using Supervisors to Keep ErlyBank Afloat | spawn_link Sat, 13 Sep 2008 14:46:27 +0000 http://spawnlink.com/?p=27#comment-61 [...] is the fourth article in the otp introduction series. If you haven’t yet, I recommend you start with the first article which talks about gen_server and lays the foundation for our bank system. If you are a quick [...] [...] is the fourth article in the otp introduction series. If you haven’t yet, I recommend you start with the first article which talks about gen_server and lays the foundation for our bank system. If you are a quick [...]

]]>