Created
August 24, 2011 04:52
-
-
Save arobson/1167335 to your computer and use it in GitHub Desktop.
Improved Approach To Limiting Asynchronous Calls with Rx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Action<IList<XElement>> saveAction = SaveChunk; | |
var loader = new BulkPostLoader(@"e:\stackoverflow\062010 so\posts.xml"); | |
var batches = loader.BufferWithCount(5000); | |
var results = batches.Select(x => saveAction.BeginInvoke(x, null, null)); | |
results | |
.Aggregate(new HashSet<IAsyncResult>(), (set, result) => | |
{ | |
set.RemoveWhere(x => x.IsCompleted); | |
set.Add(result); | |
if(set.Count > 5) | |
{ | |
var inProcess = set.Where(x => !x.IsCompleted).FirstOrDefault(); | |
if(inProcess != null) | |
{ | |
inProcess.AsyncWaitHandle.WaitOne(); | |
} | |
} | |
return set; | |
} | |
.Subscribe(x => {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment