Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Monday, March 30, 2015
( Async | MVC )

http://www.asp.net/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4

Found a strange problem with the app hanging/blocking on the await client.GetAsync call.

private static string _address = "http://www.google.com"; public async Task<ActionResult> Index() { var t1 = CallAPI(1); await t1; ViewBag.stuff = t1.Result; return View(); } async Task<string> CallAPI(int i) { string result; using (var client = new HttpClient()) { var response = await client.GetAsync(_address); response.EnsureSuccessStatusCode(); result = await response.Content.ReadAsStringAsync(); } return result; }

http://stackoverflow.com/questions/14046471/task-waitall-keeps-in-loop

http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html 

http://blog.stephencleary.com/2012/02/async-and-await.html (intro post)

| | # 
# Saturday, March 28, 2015
( Async )

http://blog.stephencleary.com/2012/02/async-and-await.html

http://stackoverflow.com/questions/14455293/async-and-await – state machine in background

http://dotnetcodr.com/2014/01/01/5-ways-to-start-a-task-in-net-c/ – 7 ways

Code demonstrating:

  • Timings
  • Task.Delay
  • Task.Run
  • An Exception
static void Main(){ //MainA().Wait(); Task t = MainA(); t.Wait(); } static async Task MainA(){ var time = DateTime.Now; var t1 = DoSomething("t1", 4000); var t2 = DoSomething("t2", 3000); var t3 = DoSomethingTaskRun("t3", 2000); var t4 = Task.Delay(6000); var t5 = DoSomethingError("t5", 2000); // Wait for t1 which is 4s, then t2 and t3 will have finished await t1; Console.WriteLine("after t1 - 4s - {0}", Actual(time)); await t2; Console.WriteLine("after t2 - 3s - {0}", Actual(time)); await t3; Console.WriteLine("after t3 - 2s - {0}", Actual(time)); // after another 2s t4 will have finished await t4; Console.WriteLine("after t4 - 6s - {0}", Actual(time)); // then immediately t5 will error try { await t5; } catch (Exception ex) { Console.WriteLine("error t5 - 2s - {0} - {1}", Actual(time), ex.Message); } // Returning data from an async method Console.WriteLine("Done: {0}", String.Join(", ", t1.Result, t2.Result, t3.Result)); Console.WriteLine("Total: " + Actual(time)); } static async Task<string> DoSomething(string name, int timeout){ var sw = new Stopwatch(); sw.Start(); await Task.Delay(timeout); var message = "Exit " + name + " in: " + sw.ElapsedMilliseconds; Console.WriteLine(message); return message; } static async Task<string> DoSomethingTaskRun(string name, int timeout) { var sw = new Stopwatch(); sw.Start(); // Await the execution until the sleep completes await Task.Run(() => System.Threading.Thread.Sleep(timeout)); var message = "Exit " + name + " in: " + sw.ElapsedMilliseconds; Console.WriteLine(message); return message; } static async Task<string> DoSomethingError(string name, int timeout) { var sw = new Stopwatch(); sw.Start(); await Task.Delay(timeout); throw new Exception("test error"); } static string Actual(DateTime time) { return (DateTime.Now - time).TotalMilliseconds.ToString(); }
| | # 
# Friday, March 27, 2015
( Async | HttpClient )

// this is their dev API key //private static string _address = "http://developer.echonest.com/api/v4/artist/biographies?api_key=FILDTEOIK2HBORODV&id=spotify:artist:4Z8W4fKeB5YxbusRsdQVPb"; static void Main() { ServicePointManager.DefaultConnectionLimit = 5; const int n = 20; var tasks = new Task<string>[n]; for (int i = 0; i < n; i++) { tasks[i] = CallAPI(i); } Task.WaitAll(tasks); Console.WriteLine("done"); Console.ReadLine(); } static async Task<string> CallAPI(int i){ var keepTrying = true; while (keepTrying){ try{ var client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(_address); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsStringAsync(); Console.WriteLine(i + result.Substring(0, 100)); keepTrying = false; return result; } catch (HttpRequestException hre){ Console.WriteLine(i + hre.Message); System.Threading.Thread.Sleep(500); } catch (Exception ex){ Console.WriteLine("here" + i + ex.Message); } } return null; }

aa

| | # 
# Thursday, March 26, 2015
( Async )

Showing running 2  tasks in series, then parallel

static void Main() { Task t = RunAsync(1); Console.WriteLine("starting 1"); Thread.Sleep(2000); Task t2 = RunAsync(2); Console.WriteLine("starting 2"); t.Wait(); Console.WriteLine("done 1"); t2.Wait(); Console.WriteLine("done 2"); } static async Task RunAsync(int i) { for (int j = 0; j < 50; j++){ Console.Write(i); // If I do this (which isn't async) then it blocks Thread.Sleep(100); //await Task.Run(() => Thread.Sleep(10)); //await Task.Delay(100); } }
| | # 
# Tuesday, March 24, 2015
( Async | Spotify )

Exploring how to call multiple webservices in parallel as many of the calls for Spotify can be made at the same time.

WebClient – since 1.1.  common methods for sending data to and receiving data from a resource identified by a URI. Simpler.
HttpWebRequest – since 1.1.  HTTP Specific implementation of WebRequest.  Allows more control
HttpClient – .NET45 or in Nuget.  Based on HttpWebRequest. Task oriented pattern for handling async requests..The benefits that HttpClient brings is with the standard classes like HttpResponseMessage, HttpRequestMessage, HttpContent and all the strongly typed headers

https://www.simple-talk.com/dotnet/.net-framework/the-.net-4.5-asyncawait-feature-in-promise-and-practice/

https://msdn.microsoft.com/en-GB/library/hh696703.aspx

image

static void Main() { //explore async/await for when I do many API calls // what if there is an error? //Sync(); Async(); Console.WriteLine("done"); } static void Sync() { var stopWatch = new Stopwatch(); stopWatch.Start(); var url = "https://api.spotify.com/v1/albums/?ids=3KuXEGcqLcnEYWnn3OEGy0,0eFHYz8NmK75zSplL5qlfM,0lw68yx3MhKflWFqCsGkIs,0HcHPBu9aaF1MxOiZmUQTl,5eJTvSeghTKoqN3Ly4TqEf,1Dh27pjT3IEdiRG9Se5uQn,6AyUVv7MnxxTuijp4WmrhO"; for (int i = 0; i < 5; i++) { Console.WriteLine("Starting " + i); HttpWebRequest request = HttpWebRequest.CreateHttp(url); string text; using (var response = request.GetResponse()) { using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); } } Console.WriteLine("result of " + i + " " + text.Substring(0,400)); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:0}", ts.TotalMilliseconds); Console.WriteLine("Total: " + elapsedTime); } static void Async() { var stopWatch = new Stopwatch(); stopWatch.Start(); ServicePointManager.DefaultConnectionLimit = 50; const int n = 5; Task<string>[] tasks = new Task<string>[n]; for (int i = 0; i < n; i++) { tasks[i] = CallAPI(i); } Task.WaitAll(tasks); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:0}", ts.TotalMilliseconds); Console.WriteLine("Total: " + elapsedTime); } // Switch to HttpClient - .NET45.. http://stackoverflow.com/a/25612028/26086 static async Task<string> CallAPI(int i) { Console.WriteLine("CallAPI enter " + i); var url = "https://api.spotify.com/v1/albums/?ids=3KuXEGcqLcnEYWnn3OEGy0,0eFHYz8NmK75zSplL5qlfM,0lw68yx3MhKflWFqCsGkIs,0HcHPBu9aaF1MxOiZmUQTl,5eJTvSeghTKoqN3Ly4TqEf,1Dh27pjT3IEdiRG9Se5uQn,6AyUVv7MnxxTuijp4WmrhO"; HttpWebRequest request = HttpWebRequest.CreateHttp(url); string text; using (var response = await request.GetResponseAsync()) { using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); } } Console.WriteLine("CallAPI exit " + i + text.Substring(0,400)); return text; }

http://stackoverflow.com/a/5827440/26086 – WebRequest will only make 2 calls to the same host – HTTP niceness!

Results

Connection limit

Sync – 9.7s
1 – 9.7s
2 – 5.3s
3 – 3.7s
4 – 3.2s
5 -  3.1s
6 – 3.0s
10 – 2.4s
20 – 2.2s
50 – 2.2s

| | # 
# Friday, March 20, 2015
( Artwork )

After the solar eclipse today this came to light:

http://www.deviantart.com/art/Eclipse-144235675

Made with Terragen

http://planetside.co.uk/

| | # 
# Wednesday, March 18, 2015