Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple parallel async calls with await

As far as I know, when runtime comes across the statement below it wraps the rest of the function as a callback to the method which is invoked asynchronously (someCall() in this example). In this case anotherCall() will be executed as a callback to someCall():

    await someCall();     await anotherCall(); 

I wonder if it is possible to make runtime perform like this: call someCall() in async fashion and return immediately to the calling thread, then invoke anotherCall() similarly (without waiting someCall to complete). Because I need these two methods to run asynchronously and suppose these calls are just fire and forget calls.

Is it possible to implement this scenario using just async and await (not using old begin/end mechanism)?

like image 855
rovsen Avatar asked May 12 '11 07:05

rovsen


People also ask

Can one async function have multiple awaits?

Even if you don't return a promise explicitly, the async function makes sure that your code is passed through a promise. await blocks the code execution within the async function, of which it ( await statement ) is a part. There can be multiple await statements within a single async function.

How do two parallel calls without blocking each other?

await Promise. all([someCall(), anotherCall()]); as already mention will act as a thread fence (very common in parallel code as CUDA), hence it will allow all the promises in it to run without blocking each other, but will prevent the execution to continue until ALL are resolved.

Can you mix promises and async await?

Yes, you can use mix await and then syntax - they both work on promises - but you shouldn't do so in the same function.


2 Answers

The async/await includes a few operators to help with parallel composition, such as WhenAll and WhenAny.

var taskA = someCall(); // Note: no await var taskB = anotherCall(); // Note: no await  // Wait for both tasks to complete. await Task.WhenAll(taskA, taskB);  // Retrieve the results. var resultA = taskA.Result; var resultB = taskB.Result; 
like image 186
Stephen Cleary Avatar answered Sep 25 '22 21:09

Stephen Cleary


The simplest way is probably to do this:

var taskA = someCall(); var taskB = someOtherCall(); await taskA; await taskB; 

This is especially nice if you want the result values:

var result = await taskA + await taskB; 

so you don't need to do taskA.Result.

TaskEx.WhenAll might be faster than two awaits after each other. i don't know since I haven't done performance investigation on that, but unless you see a problem I think the two consecutive awaits reads better, especially if you ewant the result values.

like image 44
Cellfish Avatar answered Sep 22 '22 21:09

Cellfish