c# - How to create a parallel prefetch for a foreach -


C #, TPL, parallel extensions, many new ways to perform asynchronous operations Async CTP, Reactive Extensions What I was surprised The following will be the simplest way to parallel the benefits and processing of the following:

  foreach (string url in url) {var file = FetchFile (url); ProcessFile (file); } Is   

proviso that when files can be fetched at any time ProcessFile can only file handle at a time and gradually be called.

Precisely what is a pipelined way FetchFile and ProcessFile The easiest way to achieve this?

Here RX way it will replace the steam flowing streams extend Yuri:

  Public stable IObservable & lt; Stream & gt; RequestToStream (this IObservable & LT; string & gt; source, TimeSpan timeout) {observable .FromAsyncPattern & lt return WC in source.Select (WebRequest.Create) from s; WebResponse & gt; (Wc.BeginGetResponse, wc.EndGetResponse) () .Timeout (timeout Observable.Empty & LT; WebResponse & gt; ()) .Catch (Observable.Empty & LT; WebResponse & gt; ()) s.GetResponseStream select () ; }   

Usage:

  New [] {"myuri.net \ file1.dat", "myuri.net \ file2.dat"} .ToObservable () .RequestToStream (TimeSpan.FromSeconds (5)). (Stream = & gt; process stream (stream)). Subscribe ();   

EDIT: Oops, did not require the file to be sorted. This part can be done by employing .concact which is essentially an Rx queue (second one.zip)

Let's have a .streamfile extension:

  public static IObservable & Lt; Unit & gt; Streamtophile (this is Tupal & Lt; stream, string & gt; source) {Return Overview. Check ((=) = & gt; Source. ITM 1. Async Read (). Write (File Creation (Source. Item2)); }   

You can parallel web requests now, but you can serialize the file that comes in them:

  new [] {" Myuri.net \ file1.dat "," Myuri.net \ file2.dat "}. O Oversive () .RequestToStream (TimeSpan.FromSeconds (5)) Select. ((Stream, i) => Tuple.Create (stream, i.ToString (+ + ".dat")). Select (x = & gt; x.StreamToFile ()) .cancat (). Subscribe ();    

Comments