Home > other >  C# - Creating synchronous function that uses asynchronous function
C# - Creating synchronous function that uses asynchronous function

Time:05-23

I'm using Amazon's S3 Cloud Object Storage to store files from my application. And I need to increase performance of the file uploading/downloading. From what I've seen, S3 doesn't allow you to do a GetObjects that allows you to download multiple files at once. So my idea is to create a function returns its data synchronously, but it runs asynchronously. Basically, download the files in parallel, but only return the list of files only when all the files have been downloaded.

I don't work with C# that much so I'll work some pseudo code to try to make to make it more clear:

GetObjects(listOfObjectKeys){ 
        byte listOfFiles; 
        foreach (int i in listOfObjectKeys) {
             listOfFiles.append((async)S3.GetObject(listOfObjectKeys[i]));
        }  
        return listOfFiles;  
}

Is there a way to achieve this?

Edit: forgot to add that the S3.GetObject is not an asynchronous function, I just want to know if I can call it multiple times in parallel instead of having to wait for the previous call to be over

CodePudding user response:

What you are describing (with the async etc in the example) is "sync over async", and the only correct guidance is "don't do that". If you need to call async methods: the calling code must be async - it really is as simple as that. Note also that async and parallelism are separate concepts - there is some tangential overlap, but not as much as you'd think. If what you're trying to do is parallelism, then: Parallel.ForEach etc may be useful, in the case of synchronous code. If you are talking about async code, then there are methods like Parallel.ForEachAsync. For downloads to a single backend, parallelism may or may not provide an increase in performance; if you're throttled by bandwidth on the same link, then the additional overheads may actually marginally reduce throughput (or perhaps more likely: you may get a tiny bit more throughput, but not enough to sing about).

CodePudding user response:

public static List<File> GetObjects(IEnumerable<Key> listOfObjectKeys) => listOfObjectKeys.Select(S3.GetObject).ToList().Select(x => x.Result).ToList();
  • Related