The normal two
options available are simply synchronous and asynchronous. The pseudo-code for the
synchronous version might look something like this:
HoldingsValue ComputeTotalStockValue(string user, string password)
{
Token token = AuthService.Check(user, password);
Holdings stocks = DbService.GetStockHoldings(token);
StockRates rates = StockService.GetRates(token);
return ProcessStocks(stocks, rates);
}
That??™s very simple and easy to understand, but if each request takes a second, the
whole operation will take three seconds and tie up a thread for the whole time
it??™s running. If we want to scale up to hundreds of thousands of requests running in
parallel, we??™re in trouble. Now let??™s consider a fairly simple asynchronous version,
which avoids tying up a thread when nothing??™s happening10 and uses parallel calls
where possible:
void StartComputingTotalStockValue(string user, string password)
{
AuthService.BeginCheck(user, password, AfterAuthCheck, null);
}
void AfterAuthCheck(IAsyncResult result)
{
Token token = AuthService.EndCheck(result);
IAsyncResult holdingsAsync = DbService.BeginGetStockHoldings
(token, null, null);
StockService.BeginGetRates
(token, AfterGetRates, holdingsAsync);
}
9 http://www.microsoft.com/robotics
10 Well, mostly??”in order to keep it relatively simple, it might still be inefficient as we??™ll see in a moment.
Pages:
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357