-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bridging sync/async in "hobby code" where you don't care that much #54
Comments
Lesson IV from this blog post feel pretty related:
|
I think one additional point here that I'm realizing is that fundamentally all of the code when this gets deployed is going to end up being run on a single thread, as commonly I deploy to 1-core VMs on some cloud. That might have interesting implications for the (usually) thread-aware schedulers used, and the extent to which those impose restrictions on my code. |
@seanmonstar points out that |
@seanmonstar also pointed me at seanmonstar/reqwest#1215 outlines a pretty similar scenario, except in this case (a) there was a mix of |
From the same blog post, I read that but I didn't know I get to experience the same issue as well, I didn't know it is related until @Darksonn told me so. So there is a piece of code that works on my machine but not on production, the issue is because my machine have multiple cores and the production server have single core. And a piece of code is blocking the current thread, so the production code didn't work as expected unlike locally, but for my case changing from Discussion was here https://discord.com/channels/500028886025895936/747930050674032740/825744899772121139 but other people may have experienced similar issues. Just by looking at #tokio-users discord, we can probably see many issue what others usually faced, or maybe @Darksonn can mention some common cases where people faced. |
We wrote a story outline here last Friday but nobody signed up to turn it into prose yet. Anybody interested in doing that? I may take a crack at it, just juggling a lot of things. |
I don't know if this helps whomever works on this, but here's some real code that will actually deadlock: async fn do_web_request(url: &str) -> String {
reqwest::get(url).await.unwrap().text().await.unwrap()
}
fn aggregate(urls: &[&str]) -> Vec<String> {
urls
.iter()
.map(|url| futures::executor::block_on(do_web_request(url)))
.collect()
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let result = aggregate(&["https://www.rust-lang.org", "https://google.com"]);
println!("Done! Have {} results", result.len());
} And to prove that the block_on stuff is at fault, we can re-write the async fn aggregate(urls: &[&str]) -> Vec<String> {
futures::future::join_all(
urls
.iter()
.map(|url| do_web_request(url))
).await
} |
Ah, yes. Using |
@eminence I have amended the docs a bit to add fn aggregate(urls: &[&str]) -> Vec<String> {
let handle = Handle::current();
urls.iter()
.map(|url| handle.block_on(do_web_request(url)))
.collect()
} But it got a panic as well. But the solution for my case is quite different from yours. There seemed to be many methods to do the same thing but many of them does not work, unlike the sync API the async counterparts is not foolproof enough to be working as long as it compiles. |
The problem is that blocking in async code is just fundamentally problematic. Tokio's own |
async
everywhere or addingblock_on
calls. But, at least in tokio,block_on
calls cannot execute from async threads, so this can lead to panics if that code that is usingblock_on
ever winds up in an async context.More details from my conversation with @Mark-Simulacrum
block_on
The text was updated successfully, but these errors were encountered: