'debugging multiple threads with visual studio code
I am setting some breakpoints in some async
threaded code and I can't seem to figure out how to tell visual studio code to catch any threads in this code path.
fn main() {
println!("hello world"); // hits this line when I set a breakpoint
readTheFeed("https://www.coindesk.com/arc/outboundfeeds/rss/?outputType=xml");
}
async fn readTheFeed(feed_url: &str) -> Result<Channel, Box<dyn Error>> {
let content = reqwest::get(feed_url) //doesn't hit any breakpoints in the async code
.await?
.bytes()
.await?;
let channel = Channel::read_from(&content[..])?;
println!("{}", channel.title.to_string());
println!("{}", channel.description.to_string());
Ok(channel)
}
How do I tell visual studio code to break with any threads that hit this piece of code?
EDIT ----
I updated the code as aync task are "inert" meaning they don't run unless passed to an executer. Here's the updated code:
#[tokio::main]
async fn main() {
let t = readTheFeed("https://www.coindesk.com/arc/outboundfeeds/rss/?outputType=xml");
block_on(t);
}
async fn readTheFeed(feed_url: &str) -> Result<Channel, Box<dyn Error>> {
let content = reqwest::get(feed_url)
.await?
.bytes()
.await?;
let channel = Channel::read_from(&content[..])?;
println!("{}", channel.title.to_string());
println!("{}", channel.description.to_string());
Ok(channel)
}
Now I am getting the following error:
thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', /Users/admin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.17.0/src/runtime/context.rs:21:19
Solution 1:[1]
Try this:
#[tokio::main]
async fn main() {
println!("hello world"); // hits this line when I set a breakpoint
readTheFeed("https://www.coindesk.com/arc/outboundfeeds/rss/?outputType=xml").await;
}
async fn readTheFeed(feed_url: &str) -> Result<Channel, Box<dyn Error>> {
let content = reqwest::get(feed_url) //doesn't hit any breakpoints in the async code
.await?
.bytes()
.await?;
let channel = Channel::read_from(&content[..])?;
println!("{}", channel.title.to_string());
println!("{}", channel.description.to_string());
Ok(channel)
}
The problem is that this line:
readTheFeed("https://www.coindesk.com/arc/outboundfeeds/rss/?outputType=xml");
is just creating a future and immediately dropping it without running it. In order to run the future, you need an executor (which I provide with #[tokio::main]
, but there are other ways if you need more control) and then to await
(or spawn
) the future.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |