async-await

How to wait for the result while controller is making parallel call

微笑、不失礼 提交于 2021-02-05 12:26:02
问题 I am trying to find account details from DB (GetAccountDetailAsync) for an array of accounts and would like to run in parallel to make it faster. [HttpPost] public async Task<IHttpActionResult> GetAccountsAsync(IEnumerable<int> accountIds) { var resultAccounts = new List<AccountDetail>(); var task = Task.Run(() => { Parallel.ForEach(accountIds, new ParallelOptions { MaxDegreeOfParallelism = 5 }, async accountId => { var response = await GetAccountDetailAsync(accountId).ConfigureAwait(false);

Start an async function inside a new thread

試著忘記壹切 提交于 2021-02-05 12:17:23
问题 I'm trying to create a discord bot and I need to run an async function in another new Thread since the main Thread is required to run another function (Discord Client) What I'm trying to accomplish: # This methods needs to run in another thread async def discord_async_method(): while True: sleep(10) print("Hello World") ... # Discord Async Logic # This needs to run in the main thread client.run(TOKEN) thread = "" try: # This does not work, throws error "printHelloWorld Needs to be awaited"

Start an async function inside a new thread

喜欢而已 提交于 2021-02-05 12:16:35
问题 I'm trying to create a discord bot and I need to run an async function in another new Thread since the main Thread is required to run another function (Discord Client) What I'm trying to accomplish: # This methods needs to run in another thread async def discord_async_method(): while True: sleep(10) print("Hello World") ... # Discord Async Logic # This needs to run in the main thread client.run(TOKEN) thread = "" try: # This does not work, throws error "printHelloWorld Needs to be awaited"

Async/Await is not working with node 4.x. Can i have the alternate?

两盒软妹~` 提交于 2021-02-05 11:53:22
问题 Getting below error when i try to use async/await with NodeJs 4.x. Any issue with the below sample code or should i use alternate ? async function main () { ^^^^^^^^ SyntaxError: Unexpected token function Code Samples: (async function () { const intgetIDvalue = await fntest(getID); } })(); async function fntest (getID) { return await knex .select('column1') .from('tablename') .where('ID',getID) } 回答1: try installing asyncawait. It should work for older node versions. Other alternatives are

Awaiting a sequence of tasks to run sequentially

梦想与她 提交于 2021-02-05 11:28:26
问题 I would like to create a generic method to await for a sequence of tasks to finish sequentially, retrieving the result of each one. This is the code I've created: public static class TaskMixin { public static async Task<IEnumerable<T>> AwaitAll<T>(this IEnumerable<Task<T>> tasks) { var results = new List<T>(); foreach (var t in tasks) { results.Add(await t); } return results; } } Is there a better or built-in way to do it? Notice Before writing the above method I tried with the built-in Task

PostgreSQL: command is already in progress

北城以北 提交于 2021-02-05 11:12:39
问题 My asynchronous function tries to select a single record from a table. This function accepts a few arguments passed from another function. So, some processes (6 at least) can use it simultaneously. Often I get an error with the message "command is already in progress" . I know that the problem hides in the reader, because the reader is busy when another process tries to access it. Let me publish the full code below: async private void InsertToLog(List<Printer> APrinter, List

PostgreSQL: command is already in progress

我是研究僧i 提交于 2021-02-05 11:12:35
问题 My asynchronous function tries to select a single record from a table. This function accepts a few arguments passed from another function. So, some processes (6 at least) can use it simultaneously. Often I get an error with the message "command is already in progress" . I know that the problem hides in the reader, because the reader is busy when another process tries to access it. Let me publish the full code below: async private void InsertToLog(List<Printer> APrinter, List

Queuing asynchronous task in C#

霸气de小男生 提交于 2021-02-05 09:59:49
问题 I have few methods that report some data to Data base. We want to invoke all calls to Data service asynchronously. These calls to data service are all over and so we want to make sure that these DS calls are executed one after another in order at any given time. Initially, i was using async await on each of these methods and each of the calls were executed asynchronously but we found out if they are out of sequence then there are room for errors. So, i thought we should queue all these

Using async/await with a forEach loop

那年仲夏 提交于 2021-02-05 09:39:39
问题 Are there any issues with using async / await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file. import fs from 'fs-promise' async function printFiles () { const files = await getFilePaths() // Assume this works fine files.forEach(async (file) => { const contents = await fs.readFile(file, 'utf8') console.log(contents) }) } printFiles() This code does work, but could something go wrong with this? I had someone tell me that you're not

How to use List.sort on values returned from a function that contains an async?

夙愿已清 提交于 2021-02-05 08:18:51
问题 I have this code : widget.items.sort((a, b) { await getItemDistance(a, true); await getItemDistance(b, false); return (itemADistance) .compareTo(itemBDistance); }); I am trying to sort widget.items list based on values returned from getItemDistance. However I get an error with a red squiggly line that says : The await expression can only be used in an async function and when I try to add async to the sort method I get another red squiggly line that says : The argument type 'Future Function