-
Notifications
You must be signed in to change notification settings - Fork 654
tokio_timer::Interval with compat layer is too fast #1285
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
Comments
Afaik Tokio futures only run on a tokio executor and not yet on other executors. The example however tries to drive the future from a futures |
@Matthias247 : Thanks for you reply! I didn't know Tokio has a separate executor. I am working with Futures for over a year now, but the distinction between what is Tokio and what is Futures is still very confusing to me. Do you think there still any hope to do what I'm trying to do: Using Interval with the rest of my Futures 0.3 code? If so, what is your recommendation? |
I was confused that you weren't getting an error about not being able to register the timeout with Tokio's diff --git src/main.rs src/main.rs
index 846d142..9c7ab60 100644
--- src/main.rs
+++ src/main.rs
@@ -10,11 +10,10 @@ fn main() {
let dur = Duration::new(1,0);
let interval = Interval::new(Instant::now(), dur)
- .compat()
- .map(|_| ());
+ .compat();
- let my_fut = interval.for_each(|_| {
- println!("Interval tick!");
+ let my_fut = interval.for_each(|r| {
+ println!("Interval tick!: {:?}", r);
future::ready(())
}); gives
|
@Nemo157 I see, the |
This is my current hack: use std::thread;
use std::time::{Duration, Instant};
use futures::future;
use futures::stream::StreamExt;
use futures::sink::{SinkExt};
use futures::executor::LocalPool;
use futures::channel::mpsc;
use tokio::prelude::{Sink as TokioSink, Stream as TokioStream, Future as TokioFuture};
use tokio::timer::Interval;
fn interval_thread(sender: mpsc::Sender<()>) {
let dur = Duration::new(1,0);
let interval = Interval::new(Instant::now(), dur)
.map_err(|_| ());
let my_tokio_fut = interval.for_each(move |t| {
println!("Sending tick! {:?}", t);
let c_sender = sender.clone().compat();
c_sender.send(())
.map_err(|_| ())
.map(|_| ())
});
tokio::run(my_tokio_fut);
}
fn main() {
let mut local_pool = LocalPool::new();
let (sender, receiver) = mpsc::channel::<()>(0);
thread::spawn(|| interval_thread(sender));
let my_fut = receiver.for_each(|_| {
println!("Interval tick!");
future::ready(())
});
local_pool.run_until(my_fut);
} It seems to work, but I had to spawn a new thread. |
The correct way is to use the Tokio runtime as your executor to setup the global timer instance (or manually set it up via #![feature(await_macro, async_await, futures_api)]
use tokio::timer::Interval;
use std::time::{Duration, Instant};
use futures::{FutureExt, StreamExt, TryFutureExt};
use futures::compat::Stream01CompatExt;
fn main() {
let dur = Duration::new(1,0);
let interval = Interval::new(Instant::now(), dur).compat();
let my_fut = interval.for_each(async move |a| {
println!("Interval tick!: {:?}", a);
});
tokio::run(my_fut.unit_error().boxed().compat());
} |
Closing this since the issue is that |
How to use tokio 0.2's interval in tokio 0.1's runtime? It panicked at 'timer error: timer is shutdown' I have a 0.3 future using 0.2's interval, but my runtime is 0.1's runtime, and I use compat module to convert the 0.3 future to 0.1 future, and it panicked. |
OK I see, just use a 0.1's interval and compat it as 0.3's stream to run in 0.1's runtime |
correct |
But do you know the root cause of this? I read some codes but didn't get it why it'd happen like this |
Because we use thread locals to track the current timer instance to attach the delay too. Because of how rust works when it attempts to fetch the thread local from a 0.2 runtime with a 0.1 library it will be unable to find this. |
Hi, I tried to
tokio_timer::Interval
with the compat layer of Futures 3.0. It seems like it produces ticks very fast, no matter which duration I give.Example code:
Expected behaviour: "Interval tick!" will be printed about every second.
Observed behaviour: "Interval tick!" is printed very quickly. Changing the numbers given to
Duration::new()
does not seem to change the behaviour.My Cargo.toml:
rustc version:
Your work on Futures is highly appreciated!
Thank you for your help!
The text was updated successfully, but these errors were encountered: