async-book | Asynchronous Programming in Rust | Reactive Programming library
kandi X-RAY | async-book Summary
kandi X-RAY | async-book Summary
Asynchronous Programming in Rust
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of async-book
async-book Key Features
async-book Examples and Code Snippets
Community Discussions
Trending Discussions on async-book
QUESTION
I would like to know if the answer to this rather old question about futures still applies to the more recent language constructs async/await
. It seems to be so, since code below prints:
ANSWER
Answered 2022-Feb-02 at 16:32Yes, it still applies. It fundamentally has to be that way because, like the linked answer says, each async function will be running on the same thread - std::thread::sleep
knows nothing about async, and so will make the whole thread sleep.
Nodejs (and JavaScript in general) is much more designed around async, so the language primitives and the language runtime are more async-aware in that way.
QUESTION
Minimal example of my issue.
...ANSWER
Answered 2022-Jan-05 at 11:28When you specify 'a
as a generic parameter, you mean "I premit the caller to choose any lifetime it wants". The caller may as well choose 'static
, for example. Then you promise to pass &'a mut i32
, that is, &'static mut i32
. But i
does not live for 'static
! That's the reason for the first error.
The second error is because you're promising you're borrowing i
mutably for 'a
. But again, 'a
may as well cover the entire function, even after you discarded the result! The caller may choose 'static
, for example, then store the reference in a global variable. If you use i
after, you use it while it is mutably borrowed. BOOM!
What you want is not to let the caller choose the lifetime, but instead to say "I'm passing you a reference with some lifetime 'a
, and I want you to give me back a future with the same lifetime". What we use to achieve the effect of "I'm giving you some lifetime, but let me choose which" is called HRTB (Higher-Kinded Trait Bounds).
If you only wanted to return a specific type, not a generic type, it would look like:
QUESTION
To give some context, I have the following snippet in part of my code:
...ANSWER
Answered 2021-Dec-28 at 21:25await
does not block the current thread. It yields to another coroutine on the same thread. thread::sleep
does block the current thread, which is why you should never use it in async code. Instead, you'd use something like tokio::time::sleep
, which is async and yields. As the book says:
Whereas calling a blocking function in a synchronous method would block the whole thread, blocked Futures will yield control of the thread, allowing other Futures to run.
That said, if you need to wait 1 second for your test to pass, you may need to rethink the order of operations in your close. That may be indicating a race condition. I'm not familiar with which crate you're using here to know if this is expected for a unit test.
QUESTION
I'm trying to get data using crates_io_api
.
I attempted to get data from a stream, but
I can not get it to work.
AsyncClient::all_crates
returns an impl Stream
. How do I get data from it? It would be helpful if you provide code.
I checked out the async book but it didn't work. Thank you.
Here's my current code.
...ANSWER
Answered 2021-May-07 at 17:31This looks like a mistake on the side of crates_io_api
. Getting the next
element of a Stream
requires that the Stream
is Unpin
:
QUESTION
pub async fn send_and_expect(&mut self, request: rtsp_types::Request, retrying: bool) -> std::result::Result, ClientActionError> {
...ANSWER
Answered 2021-Apr-10 at 06:16I found [...] but it is for a function that does not use
async
.
Yes it does. You should take a closer look at code. The original function is definitely async
:
QUESTION
I have a struct G like this
...ANSWER
Answered 2021-Feb-15 at 03:54I've solved Question 3 with a lot of if
statements. I hope there are some more elegant implementations. And, I really appreciate any learning material as stated in Question 1.
here's the full code that compiles (simplified):
QUESTION
I am finding it difficult to understand why and when I need to explicitly do something with the Context
and/or its Waker
passed to the poll
method on an object for which I am implementing Future
. I have been reading the documentation from Tokio and the Async Book, but I feel the examples/methods are too abstract to be applied to real problems.
For example, I would have thought the following MRE would deadlock since the future generated by new_inner_task
would not know when a message has been passed on the MPSC channel, however, this example seems to work fine. Why is this the case?
ANSWER
Answered 2021-Feb-11 at 11:50You are passing the same Context
(and thus Waker
) to the poll()
method of the Future returned by new_inner_task
, which passes it down the chain to the poll()
of the Future
returned by UnboundedReceiverStream::next()
. The implementation of that arranges to call wake()
on this Waker
at the appropriate time (when new elements appear in the channel). When that is done, Tokio polls the top-level future associated with this Waker
- the join!()
of the three futures.
If you omitted the line that polls the inner task and just returned Poll::Pending
instead, you would get the expected situation, where your Future
would be polled once and then "hang" forever, as nothing would wake it again.
QUESTION
In the documentation for the Send
trait, there is a nice example of how something like Rc is not Send
, since cloning/dropping in two different threads can cause the reference count to get out of sync.
What is less clear, however, is why holding a binding to a non-Send
type across an await
point in an async fn
causes the generated future to also be non-Send
. I was able to find a work around for when the compiler has been too conservative in the work-arounds chapter of the async handbook, but it does not go as far as answering the question that I am asking here.
Perhaps someone could shed some light on this with an example of why having a non-Send
type in a Future
is ok, but holding it across an await
is not?
ANSWER
Answered 2021-Feb-05 at 17:27When you use .await
in an async function, the compiler builds a state machine behind the scenes. Each .await
introduces a new state (while it waits for something) and the code in between are state transitions (aka tasks), which will be triggered based on some external event (e.g. from IO or a timer etc).
Each task gets scheduled to be executed by the async runtime, which could choose to use a different thread from the previous task. If the state transition is not safe to be sent between threads then the resulting Future
is also not Send
so that you get a compilation error if you try to execute it in a multi-threaded runtime.
It is completely OK for a Future
not to be Send
, it just means you can only execute it in a single-threaded runtime.
Perhaps someone could shed some light on this with an example of why having a non-
Send
type in aFuture
is ok, but holding it across anawait
is not?
Consider the following simple example:
QUESTION
ANSWER
Answered 2020-Aug-07 at 12:55According to justinas the answer is:
QUESTION
I'm working on an asynchronous Rust program but the join!
macro doesn't work. .await
also does not work. Where is my problem ?
I saw examples in the official documentation.
This also doesn't work:
...ANSWER
Answered 2020-Mar-18 at 23:55Your problem can be reduced to this:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install async-book
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page