Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:13:36+00:00 2026-05-27T13:13:36+00:00

I have a WebRole running on a small instance. This WebRole has a method

  • 0

I have a WebRole running on a small instance. This WebRole has a method that uploads a large amount of files to BLOB storage. According to the Azure instances specs, a small instance has only 1 core. So when uploading those blobs, will Parallel.Foreach give me any benefits over a regular Foreach ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:13:36+00:00Added an answer on May 27, 2026 at 1:13 pm

    You would be much better served by focusing on using the aysnc versions of the blob storage APIs and/or Stream APIs so that you are I/O bound rather than CPU bound. Anywhere there is a BeginXXX API you should use it by wrapping it up with Task.Factory.FromAsync and the using a continuation from there. In your specific case you should leverage CloudBlob.BeginUploadFromStream. How you get the stream initially is just as important so look for async APIs on that end too.

    The only thing that may hold you back from using a small instance after that is that it’s capped at 100Mbps where as medium is 200Mbps. Then again you can always leverage the elasticity factor and increase role count when you need more processing and scale back again when things calm down.

    Here’s an example of how you would call BeginUploadFromStream using FromAsync. Now, as far as coordinating concurrent processing, since you’re now kicking off async tasks you can’t count on Parallel::ForEach to constrain the max concurrency for you. This means you will just have a regular foreach on the original thread with a Semaphore to limit concurrency. This will provide the equivalent of MaxDegreeOfParallelism:

    // Setup a semaphore to constrain the max # of concurrent "thing"s we will process
    int maxConcurrency = ... read from config ...
    Semaphore maxConcurrentThingsToProcess = new Semaphore(maxConcurrency, maxConcurrency);
    
    // Current thread will enumerate and dispatch I/O work async, this will be the only CPU resource we're holding during the async I/O
    foreach(Thing thing in myThings)
    {
        // Make sure we haven't reached max concurrency yet
        maxConcurrentThingsToProcess.WaitOne();
    
        try
        {
            Stream mySourceStream = ... get the source stream from somewhere ...;
            CloudBlob myCloudBlob = ... get the blob from somewhere ...;
    
            // Begin uploading the stream asynchronously
            Task uploadStreamTask = Task.Factory.FromAsync(
                myCloudBlob.BeginUploadFromStream,
                myCloudBlob.EndUploadFromStream,
                mySourceStream,
                null);
    
            // Setup a continuation that will fire when the upload completes (regardless of success or failure)
            uploadStreamTask.ContinueWith(uploadStreamAntecedent =>
            {
                try
                {
                    // upload completed here, do any cleanup/post processing
                }
                finally
                {
                    // Release the semaphore so the next thing can be processed
                    maxConcurrentThingsToProcess.Release();
                }
            });
        }
        catch
        {
            // Something went wrong starting to process this "thing", release the semaphore
            maxConcurrentThingsToProcess.Release();
    
            throw;
        }
    }
    

    Now in this sample I am not showing how you should also be getting the source stream asynchronously, but if, for example, you were downloading that stream from a URL someplace else, you would want to kick that off asynchronously as well and chain the starting of the async upload here into a continuation on that.

    Believe me, I know this is more code than just doing a simple Parallel::ForEach, but Parallel::ForEach exists to make concurrency for CPU bound tasks easy. When it comes to I/O, using the async APIs is the only way to achieve maximum I/O throughput while minimizing CPU resources.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MVC project on Windows Azure. This project has WebRole with method OnStart.
I have an MVC web app running in azure that serves up large files
I have a problem related to the webRole debugging. Not running in a hosted
Lets say I have a Silverlight application that currently talks to a WebRole in
We have WebRole, that hosting multiple sites. We made startup tasks for each site
I know that you can only have 1 web role per instance but does
We have a Web Role that we are hosting in Windows Azure that uses
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a WCF application hosted as a webrole in Azure with the following
I have stored some files in the form of blobs on azure and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.