You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's a demo of using spawn() to run some timeouts:
use ctrlc;use smol::channel;use smol::Timer;use std::time::Duration;asyncfnwait_for_signal<T>(receiver:&channel::Receiver<T>){// Receive a message that indicates the Ctrl-C signal occurred.
receiver.recv().await.ok();println!("Signal received");}asyncfnwait_for_timeout(seconds:u64,text:&str){Timer::after(Duration::from_secs(seconds)).await;println!("Timeout: {}", text);}fnmain(){// Set a handler that sends a message through a channel.let(sender, ctrl_c) = channel::unbounded();let handle = move || {
sender.try_send(()).ok();};
ctrlc::set_handler(handle).unwrap();println!("Waiting for Ctrl-C...");
smol::block_on(async{
smol::spawn(wait_for_timeout(4,"four")).detach();
smol::spawn(wait_for_timeout(3,"three")).detach();
smol::spawn(wait_for_timeout(2,"two")).detach();
smol::spawn(wait_for_timeout(1,"one")).detach();wait_for_signal(&ctrl_c).await;})}
It is included in this library for convenience when writing unit tests and small programs, but it is otherwise more advisable to create your own Executor.
So I tried replacing smol::spawn with my own executor:
use std::time::Duration;use ctrlc;use smol::{channel,Executor,Timer};asyncfnwait_for_signal<T>(receiver:&channel::Receiver<T>){// Receive a message that indicates the Ctrl-C signal occurred.
receiver.recv().await.ok();println!("Signal received");}asyncfnwait_for_timeout(seconds:u64,text:&str){Timer::after(Duration::from_secs(seconds)).await;println!("Timeout: {}", text);}fnmain(){// Set a handler that sends a message through a channel.let(sender, ctrl_c) = channel::unbounded();let handle = move || {
sender.try_send(()).ok();};
ctrlc::set_handler(handle).unwrap();println!("Waiting for Ctrl-C...");
smol::block_on(async{let ex = Executor::new();
ex.spawn(wait_for_timeout(4,"four")).detach();
ex.spawn(wait_for_timeout(3,"three")).detach();
ex.spawn(wait_for_timeout(2,"two")).detach();
ex.spawn(wait_for_timeout(1,"one")).detach();wait_for_signal(&ctrl_c).await;})}
Now nothing is printed. Ctrl+C still works. I assume there is something else I have to do to get the executor working, but it's not documented what. The docs for Executor use something called easy_parallel which isn't part of smol.
System: rustc 1.49, Ubuntu 20.10, smol 1.2.5.
The text was updated successfully, but these errors were encountered:
Here's a demo of using
spawn()
to run some timeouts:It should print:
And exit (early, even) if you press Ctrl+C.
The docs for
smol::spawn()
say:So I tried replacing
smol::spawn
with my own executor:Now nothing is printed. Ctrl+C still works. I assume there is something else I have to do to get the executor working, but it's not documented what. The docs for Executor use something called
easy_parallel
which isn't part of smol.System: rustc 1.49, Ubuntu 20.10, smol 1.2.5.
The text was updated successfully, but these errors were encountered: