HELP! tokio::test and tokio::main behave differently #3339
Unanswered
kaixinbaba
asked this question in
Q&A
Replies: 1 comment 1 reply
-
This is because you are blocking the thread and the test runtime is single-threaded. Consider reading Async: What is blocking?. As for the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I found the same code, but the performance is different with test run or main run, the following is an example:
Here's the output from console by execute
fn main
:And here's the output by
fn new_test
:I have two question:
tokio::spawn
is not executed at all iffn new_test
tokio::spawn
, mymain
function won't stopSo, WHY? Did I miss something?
And now I update the code, add
await
And now both
new_test
andmain
output are same:BUT!
The code after
await
will never be executed, becausespawn
is a loop and will not return, so the code is blocked atawait
。My goal is to use a thread loop to process the data from the channel, and the main thread is responsible for sending data to the channel, and the child thread should automatically exit when the main thread ends. How can I implement similar functions in tokio by RUST, I am Rust NOOB, Please help!
I also put this in issue
issue-3337
Beta Was this translation helpful? Give feedback.
All reactions