async-await

How to implement a `Future` / `Stream` that polls `async fn(&mut self)`?

↘锁芯ラ 提交于 2020-08-24 11:25:01
问题 I have the following struct struct Test; impl Test { async fn function(&mut self) {} } I want to implement an std::future::Future (well, actually futures::Stream , but it's basically the same) on Test , that would poll the function . My first try looked something like this impl Future for Test { type Output = (); fn poll(self: Pin<&mut self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.function() { Poll::Pending => Poll::Pending, Poll::Ready(_) => Poll::Ready(()), } } } Obviously

How to implement a `Future` / `Stream` that polls `async fn(&mut self)`?

安稳与你 提交于 2020-08-24 11:24:41
问题 I have the following struct struct Test; impl Test { async fn function(&mut self) {} } I want to implement an std::future::Future (well, actually futures::Stream , but it's basically the same) on Test , that would poll the function . My first try looked something like this impl Future for Test { type Output = (); fn poll(self: Pin<&mut self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.function() { Poll::Pending => Poll::Pending, Poll::Ready(_) => Poll::Ready(()), } } } Obviously

How to wait for a list of async function calls in rust?

喜你入骨 提交于 2020-08-20 12:01:31
问题 I have a list of async functions in rust that I want to execute concurrently and then wait for all them to finish. The working code I have right now is async fn start_consumers(&self) { for consumer in &self.consumers { consumer.consume().await; } } This is not quite accurate as the functions are executed serially. I am looking for something like join! , but which works on a dynamic vector, Using which I should be able to write something like async fn start_consumers(&self) { let mut v = Vec:

Flutter - How do I use await inside the streambuilder?

一世执手 提交于 2020-08-20 11:32:21
问题 I want to use await inside streambuilder. However, if you use async inside, you get an error. On the code below !!!!!!!! That's the part I want to solve. Thank you very much if I can tell you how. class _MemoStreamState extends State<MemoStream> { final _fireStore = Firestore.instance; @override Widget build(BuildContext context) { return StreamBuilder<QuerySnapshot>( stream: _fireStore .collection(widget.logInUsrEmail) .orderBy('id', descending: false) .snapshots(), builder: (context,

C# async task completes before it's finished

拥有回忆 提交于 2020-08-20 05:56:26
问题 I'm developing a network application that receives data from a websocket, modifies it, and uploads it to a data service. Uploading the data takes some time and I'd like to hide that latency, uploading several messages at once. When an upload is complete I need to send an acknowledgement back through the websocket. I've created a work queue to hold all of the outstanding tasks. This seems to be working well, so I haven't included it. But my upload task appears to be finishing before it's

What is a Future and how do I use it?

纵然是瞬间 提交于 2020-08-13 05:29:25
问题 I get the following error: A value of type 'Future<int>' can't be assigned to a variable of type 'int' It might be another type instead of int , but basically the pattern is A value of type 'Future<T>' can't be assigned to a variable of type 'T' So... What exactly is a Future ? How do I get the actual value I want to get? What widget do I use to display my value when all I have is a Future<T> ? 回答1: In case you are familiar with Task<T> or Promise<T> and the async / await pattern, then you

Do async and await produce acquire and release semantics?

守給你的承諾、 提交于 2020-08-10 05:29:05
问题 I couldn't find a clear answer about whether returning from an async method always produces release semantics and whether await always produces acquire semantics. I assume yes, because otherwise any async/await code would be a minefield? So here's an example: are returned values both guaranteed to be 100*2 and 12345*2 , without any explicit locks or barriers? private static async Task<(int, int)> AMethod() { // Runs on the original thread: var x = 100; var y = 12345; var task = Task.Run(() =>

How to synchronously upload files to S3 using aws-sdk?

一个人想着一个人 提交于 2020-08-08 18:16:31
问题 I'm attempting to upload files to my S3 bucket and then return out of my upload function. The problem is that I'm returning out of the function before the upload returns the stored data. I've attempted to use async/await with s3.upload , but I don't believe s3.upload is a promise so it doesn't do anything. ex: for (const file of files) { const params = { Bucket: BUCKET_NAME, Key: file.name, Body: file.data }; const stored = await s3.upload(params, (err, data) => { if (err) console.log("error"

NodeJS How To Handle Concurrent Request To MySQL

隐身守侯 提交于 2020-08-08 05:24:01
问题 I am having a problem with NodeJS. When I am trying to post/update One balance One account posting from Two different users at the same time. Example: I have API with ExpressJS for post data to the t_account table like this. I need to get latest balance first from max(id) after each transaction. I'm trying to get balance of 400 NOT 300 For Example: Starting Balance = 100 1st new post = 100, => balance =200 2nd new post = 200, => balance =400 Code Example: router.post('/saveBalance',async

NodeJS How To Handle Concurrent Request To MySQL

拟墨画扇 提交于 2020-08-08 05:23:29
问题 I am having a problem with NodeJS. When I am trying to post/update One balance One account posting from Two different users at the same time. Example: I have API with ExpressJS for post data to the t_account table like this. I need to get latest balance first from max(id) after each transaction. I'm trying to get balance of 400 NOT 300 For Example: Starting Balance = 100 1st new post = 100, => balance =200 2nd new post = 200, => balance =400 Code Example: router.post('/saveBalance',async