async-await

Add delay to parallel API call

六眼飞鱼酱① 提交于 2021-01-27 07:33:21
问题 I'm using Polly to make parallel API calls. The server however can't process more than 25 calls per second and so I'm wondering if there is a way to add a 1s delay after each batch of 25 calls? var policy = Policy .Handle<HttpRequestException>() .RetryAsync(3); foreach (var mediaItem in uploadedMedia) { var mediaRequest = new HttpRequestMessage { *** } async Task<string> func() { var response = await client.SendAsync(mediaRequest); return await response.Content.ReadAsStringAsync(); } tasks

Add delay to parallel API call

烂漫一生 提交于 2021-01-27 07:28:41
问题 I'm using Polly to make parallel API calls. The server however can't process more than 25 calls per second and so I'm wondering if there is a way to add a 1s delay after each batch of 25 calls? var policy = Policy .Handle<HttpRequestException>() .RetryAsync(3); foreach (var mediaItem in uploadedMedia) { var mediaRequest = new HttpRequestMessage { *** } async Task<string> func() { var response = await client.SendAsync(mediaRequest); return await response.Content.ReadAsStringAsync(); } tasks

Awaitable type in TypeScript

情到浓时终转凉″ 提交于 2021-01-27 07:02:38
问题 I use async / await a lot in JavaScript. Now I’m gradually converting some parts of my code bases to TypeScript. In some cases my functions accept a function that will be called and awaited. This means it may either return a promise, just a synchronous value. I have defined the Awaitable type for this. type Awaitable<T> = T | Promise<T>; async function increment(getNumber: () => Awaitable<number>): Promise<number> { const num = await getNumber(); return num + 1; } It can be called like this:

Awaitable type in TypeScript

柔情痞子 提交于 2021-01-27 07:01:27
问题 I use async / await a lot in JavaScript. Now I’m gradually converting some parts of my code bases to TypeScript. In some cases my functions accept a function that will be called and awaited. This means it may either return a promise, just a synchronous value. I have defined the Awaitable type for this. type Awaitable<T> = T | Promise<T>; async function increment(getNumber: () => Awaitable<number>): Promise<number> { const num = await getNumber(); return num + 1; } It can be called like this:

Where does async and await end? Confusion

大憨熊 提交于 2021-01-27 06:17:13
问题 I have a program which has no purpose but to help me understand how async and await works. It's a console application which parses XML and waits for a name to be returned, either the surname or the first name. Here is the code: static void Main(string[] args) { Task<string> name = GetFirstParsedName(); name.Wait(); if (name.IsCompleted) { Console.WriteLine(name.Result); } Console.ReadLine(); } static async Task<string> GetFirstParsedName() { string xmlSnippet = @"<person> <FirstName>Chamir<

Where does async and await end? Confusion

拜拜、爱过 提交于 2021-01-27 06:17:13
问题 I have a program which has no purpose but to help me understand how async and await works. It's a console application which parses XML and waits for a name to be returned, either the surname or the first name. Here is the code: static void Main(string[] args) { Task<string> name = GetFirstParsedName(); name.Wait(); if (name.IsCompleted) { Console.WriteLine(name.Result); } Console.ReadLine(); } static async Task<string> GetFirstParsedName() { string xmlSnippet = @"<person> <FirstName>Chamir<

System.Timers.Timer Elapsed intermittently not firing when using an Task.Run with async from Console App

给你一囗甜甜゛ 提交于 2021-01-26 13:23:49
问题 I am using a console application and I have batches of 20 URIs that I need to read from and I have found a massive speed boost by making all tasks and running them in parallel then sorting the results on completion in a different thread (allowing the next batch to be fetched). In the calls I am currently using, each thread blocks when it gets the response stream, I also see there is a async version of the same method GetResponseAsync . I understand there are benefits of freeing up the thread

System.Timers.Timer Elapsed intermittently not firing when using an Task.Run with async from Console App

孤者浪人 提交于 2021-01-26 13:23:19
问题 I am using a console application and I have batches of 20 URIs that I need to read from and I have found a massive speed boost by making all tasks and running them in parallel then sorting the results on completion in a different thread (allowing the next batch to be fetched). In the calls I am currently using, each thread blocks when it gets the response stream, I also see there is a async version of the same method GetResponseAsync . I understand there are benefits of freeing up the thread

System.Timers.Timer Elapsed intermittently not firing when using an Task.Run with async from Console App

大憨熊 提交于 2021-01-26 13:23:07
问题 I am using a console application and I have batches of 20 URIs that I need to read from and I have found a massive speed boost by making all tasks and running them in parallel then sorting the results on completion in a different thread (allowing the next batch to be fetched). In the calls I am currently using, each thread blocks when it gets the response stream, I also see there is a async version of the same method GetResponseAsync . I understand there are benefits of freeing up the thread

System.Timers.Timer Elapsed intermittently not firing when using an Task.Run with async from Console App

早过忘川 提交于 2021-01-26 13:23:05
问题 I am using a console application and I have batches of 20 URIs that I need to read from and I have found a massive speed boost by making all tasks and running them in parallel then sorting the results on completion in a different thread (allowing the next batch to be fetched). In the calls I am currently using, each thread blocks when it gets the response stream, I also see there is a async version of the same method GetResponseAsync . I understand there are benefits of freeing up the thread