{"id":12930,"date":"2022-10-02T11:29:55","date_gmt":"2022-10-02T05:59:55","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/"},"modified":"2022-10-02T11:29:55","modified_gmt":"2022-10-02T05:59:55","slug":"solved-downloading-1000-files-fast","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/","title":{"rendered":"[Solved] Downloading 1,000+ files fast?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-49972755\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"49972755\" data-parentid=\"49972447\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h1>Update<\/h1>\n<p>It was just pointed out to me in a comment by <strong>Jimi<\/strong>, that <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms144196(v=vs.110).aspx\">DownloadFileAsync<\/a> is an event driven call and not awaitable. Though, there is a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.net.webclient.downloadfiletaskasync(v=vs.110).aspx\">WebClient.DownloadFileTaskAsync<\/a> version, which would be the appropriate one to use in this example, it is an awaitable call and returns a <code>Task<\/code><\/p>\n<blockquote>\n<p>Downloads the specified resource to a local file as an asynchronous<br \/>\noperation using a task object.<\/p>\n<\/blockquote>\n<h1>Original answer<\/h1>\n<blockquote>\n<p>I know I could run multiple threads or even parallel but what&#8217;s the<br \/>\nbest way<\/p>\n<\/blockquote>\n<p>Yes you can make it parallel and be in control of the resources you use.<\/p>\n<blockquote>\n<p>I&#8217;m not too worried about speed as long as it isn&#8217;t as slow as right<br \/>\nnow, but I don&#8217;t want to overpower the device&#8217;s resources such as CPU<br \/>\ntrying to speed it up<\/p>\n<\/blockquote>\n<p>You should be able to achieve this and configure this fairly well.<\/p>\n<hr>\n<p>OK, so there are many ways to do this. Here are some things to think about:<\/p>\n<ul>\n<li>You have 1000s of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/I\/O_bound\">IO bound<\/a> tasks (as opposed to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/CPU-bound\">CPU bound<\/a> tasks)<\/li>\n<li>With this many files, you want sort of parallelism and to be able to to configure the amount of concurrent tasks.<\/li>\n<li>You <strong>will<\/strong> want to do this in an <code>async<\/code> \/ <code>await<\/code> pattern so you&#8217;re not wasting system resources on IO completion ports or smashing your CPU<\/li>\n<\/ul>\n<p>Some immediate solutions:<\/p>\n<ul>\n<li>Tasks, and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd270695(v=vs.110).aspx\"><code>WaitAll<\/code><\/a> in an <code>asnyc<\/code> \/ <code>await<\/code> pattern, this is a great approach however it&#8217;s a little bit trickier to limit concurrent tasks.<\/li>\n<li>You have the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.threading.tasks.parallel.foreach?view=netframework-4.7.2\"><code>Parallel.ForEach<\/code><\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.threading.tasks.parallel.for?view=netframework-4.7.2\"><code>Parallel.For<\/code><\/a>, this has a nice approach to limit concurrent workloads, but its just not suited to IO bound tasks<\/li>\n<li>Or another option you might consider is the <em>Microsoft<\/em> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/parallel-programming\/dataflow-task-parallel-library\">Dataflow (Task Parallel Library)<\/a>, I have come to like these libraries a lot lately as they can give you the best of both worlds.<\/li>\n<\/ul>\n<p><strong>Please note<\/strong>: there are many other approaches.<\/p>\n<p>So <code>Parallel.ForEach<\/code> uses the thread pool. Moreover, <em>IO bound<\/em> operations will block those threads waiting for a device to respond and tie up resources. A general rule of thumb here is<\/p>\n<ul>\n<li>If you have <em>CPU bound<\/em> code, <code>Parallel.ForEach<\/code> is appropriate;<\/li>\n<li>Though if you have <em>IO bound<\/em> code, <em>Asynchrony<\/em> is appropriate.<\/li>\n<\/ul>\n<p>In this case, downloading a file is clearly <em>I\/O<\/em>, there is a <code>DownloadFileAsync<\/code> version, and 1000 files to download, so you are best to use <code>async<\/code>\/<code>await<\/code> pattern and some type of limit on concurrent tasks<\/p>\n<hr>\n<p>Here is a very basic example of how you might achieve this:<\/p>\n<p><strong>Given<\/strong><\/p>\n<pre><code>public class WorkLoad\n{\n    public string Url {get;set;}\n    public string FileName {get;set;}\n\n}\n<\/code><\/pre>\n<p><strong>Dataflow example<\/strong><\/p>\n<pre><code>public async Task DoWorkLoads(List&lt;WorkLoad&gt; workloads)\n{\n   var options = new ExecutionDataflowBlockOptions\n                     {\n                        \/\/ add pepper and salt to taste\n                        MaxDegreeOfParallelism = 50\n                     };\n\n   \/\/ create an action block\n   var block = new ActionBlock&lt;WorkLoad&gt;(MyMethodAsync, options);\n\n   \/\/ Queue them up\n   foreach (var workLoad in workloads)\n      block.Post(workLoad );\n\n   \/\/ wait for them to finish\n   block.Complete();\n   await block.Completion;\n\n}\n\n...\n\n\/\/ Notice we are using the async \/ await pattern\npublic async Task MyMethodAsync(WorkLoad workLoad)\n{\n   \n    try\n    {\n        Console.WriteLine(\"Downloading: \" + workLoad.Url);\n        await client.DownloadFileAsync(workLoad.Url, workLoad.FileName);\n    }\n    catch (Exception)\n    {\n        \/\/ probably best to add some error checking some how\n    }\n}\n<\/code><\/pre>\n<p><strong>Summary<\/strong><\/p>\n<p>This approach gives you <em>Asynchrony<\/em>, it also gives you <code>MaxDegreeOfParallelism<\/code>, it doesn&#8217;t waste resources, and lets <em>IO<\/em> be <em>IO<\/em><\/p>\n<p><strong>Disclaimer<\/strong>, DataFlow may not be where you want to be, however I just thought I&#8217;d give you some more information<\/p>\n<p><strong>Disclaimer 2<\/strong>, Also the above code has not been tested, I would seriously consider researching this technology first and doing your on due diligence thoroughly.<\/p>\n<hr>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/dotnetfiddle.net\/Zp4FEB\"><strong>Loosely related demo here<\/strong><\/a><\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Downloading 1,000+ files fast? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Update It was just pointed out to me in a comment by Jimi, that DownloadFileAsync is an event driven call and not awaitable. Though, there is a WebClient.DownloadFileTaskAsync version, which would be the appropriate one to use in this example, it is an awaitable call and returns a Task Downloads the specified resource to &#8230; <a title=\"[Solved] Downloading 1,000+ files fast?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\" aria-label=\"More on [Solved] Downloading 1,000+ files fast?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[352,324],"class_list":["post-12930","post","type-post","status-publish","format-standard","hentry","category-solved","tag-net","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Downloading 1,000+ files fast? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Downloading 1,000+ files fast? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Update It was just pointed out to me in a comment by Jimi, that DownloadFileAsync is an event driven call and not awaitable. Though, there is a WebClient.DownloadFileTaskAsync version, which would be the appropriate one to use in this example, it is an awaitable call and returns a Task Downloads the specified resource to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-02T05:59:55+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Downloading 1,000+ files fast?\",\"datePublished\":\"2022-10-02T05:59:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\"},\"wordCount\":482,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\".net\",\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\",\"name\":\"[Solved] Downloading 1,000+ files fast? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-02T05:59:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Downloading 1,000+ files fast?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Downloading 1,000+ files fast? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Downloading 1,000+ files fast? - JassWeb","og_description":"[ad_1] Update It was just pointed out to me in a comment by Jimi, that DownloadFileAsync is an event driven call and not awaitable. Though, there is a WebClient.DownloadFileTaskAsync version, which would be the appropriate one to use in this example, it is an awaitable call and returns a Task Downloads the specified resource to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/","og_site_name":"JassWeb","article_published_time":"2022-10-02T05:59:55+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Downloading 1,000+ files fast?","datePublished":"2022-10-02T05:59:55+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/"},"wordCount":482,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":[".net","c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/","url":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/","name":"[Solved] Downloading 1,000+ files fast? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-02T05:59:55+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-downloading-1000-files-fast\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Downloading 1,000+ files fast?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=12930"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12930\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=12930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=12930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=12930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}