async-await

How to delay 'hot' tasks so they can processed in a set order

有些话、适合烂在心里 提交于 2019-12-19 11:19:50
问题 Say I have a set of tasks: var task1 = DoThisAsync(...); var task2 = DoThatAsync(...); var task3 = DoOtherAsync(...); var taskN... I am looking for a way to process a set of tasks in order (determined by place in containing collection say), but to have the tasks only run/start when its their turn and not before - and have all of that wrapped up in its own task. Problem constraints / details are: These tasks need to be performed in a certain order i.e.task1, task2,... The previous task must

How to avoid using await key in dart Map by foreach function

旧街凉风 提交于 2019-12-19 10:49:23
问题 So, I have a map which has to do with some asynchronous processing using the items inside. I used the forEach loop construct and inside the callback is designed to be async because I call an await inside the iteration body myMap.forEach((a, b) { await myAsyncFunc(); } ); callFunc(); I need the callFunc() to be called after all the items have been iterated. But the forEach exits immediately. Help! 回答1: Use a for loop over Map.entries instead of forEach. Provided you are in an async function,

How does the asyncio module work, why is my updated sample running synchronously?

对着背影说爱祢 提交于 2019-12-19 10:12:31
问题 I have tried the following code in Python 3.6 for asyncio: Example 1: import asyncio import time async def hello(): print('hello') await asyncio.sleep(1) print('hello again') tasks=[hello(),hello()] loop=asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) Output is as expected: hello hello hello again hello again Then I want to change the asyncio.sleep into another def: async def sleep(): time.sleep(1) async def hello(): print('hello') await sleep() print('hello again')

Async wait block Main UI

这一生的挚爱 提交于 2019-12-19 09:49:20
问题 I am using the new async await features to upgrade from backgroundworker in C#. In the following code I am trying to replicate the execution of multiple tasks with ContinueWith method. Task t1 = new Task ( () => { Thread.Sleep(10000); // make the Task throw an exception MessageBox.Show("This is T1"); } ); Task t2 = t1.ContinueWith ( (predecessorTask) => { if (predecessorTask.Exception != null) { MessageBox.Show("Predecessor Exception within Continuation"); return; } Thread.Sleep(1000);

Using Task.Yield to overcome ThreadPool starvation while implementing producer/consumer pattern

社会主义新天地 提交于 2019-12-19 08:38:06
问题 Answering the question: Task.Yield - real usages? I proposed to use Task.Yield allowing a pool thread to be reused by other tasks. In such pattern: CancellationTokenSource cts; void Start() { cts = new CancellationTokenSource(); // run async operation var task = Task.Run(() => SomeWork(cts.Token), cts.Token); // wait for completion // after the completion handle the result/ cancellation/ errors } async Task<int> SomeWork(CancellationToken cancellationToken) { int result = 0; bool loopAgain =

Async method call in Razor view

限于喜欢 提交于 2019-12-19 08:04:04
问题 I have @Html.Translate() extension method, which returns translated string. Translations are stored in database, so I want to make async call to database from helper method. Can I make async method calls from razor view? Is here some workaround to call async methods in razor view? 回答1: Async method calls from razor view are supported (or will be supported) only in ASP.NET vNext (MVC 6). If you are using MVC 5 version and eralier, the simplest solution to your problem would be adding a

Async method call in Razor view

谁说我不能喝 提交于 2019-12-19 08:02:25
问题 I have @Html.Translate() extension method, which returns translated string. Translations are stored in database, so I want to make async call to database from helper method. Can I make async method calls from razor view? Is here some workaround to call async methods in razor view? 回答1: Async method calls from razor view are supported (or will be supported) only in ASP.NET vNext (MVC 6). If you are using MVC 5 version and eralier, the simplest solution to your problem would be adding a

ASP.NET async/await part 2

扶醉桌前 提交于 2019-12-19 07:56:17
问题 I have a variation of the benefits-of-async/await-on-ASP.NET from this question. My understanding is that asynchrony is not the same thing as parallelism. So, on a web server, I wonder about how much benefit async/await brings to ASP.NET pages. Isn't IIS+ASP.NET already really good at allocating threads for requests, and if onen page is busy waiting for a resource the server will just switch to processing another request that has work to do? There are a limited number of threads in the pool

ASP.NET async/await part 2

折月煮酒 提交于 2019-12-19 07:56:08
问题 I have a variation of the benefits-of-async/await-on-ASP.NET from this question. My understanding is that asynchrony is not the same thing as parallelism. So, on a web server, I wonder about how much benefit async/await brings to ASP.NET pages. Isn't IIS+ASP.NET already really good at allocating threads for requests, and if onen page is busy waiting for a resource the server will just switch to processing another request that has work to do? There are a limited number of threads in the pool

Cancellation Token in await method

ぐ巨炮叔叔 提交于 2019-12-19 07:45:23
问题 There are many reasons to put a token in the constructor of a task, mentioned here: Cancellation token in Task constructor: why? With the use of keywords, async / await, how is that working? for example my code below: public async Task MethodAsync(CancellationToken token) { await Method01Async(); await Method02Async(); } Although it is an asynchronous process. In no time I used "Task.StartNext" or "Task.Run" or "new Task". To be able to specify my cancellation token, how can I do? 回答1: You