Skip to content

Commit

Permalink
Remove all usages of tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Apr 22, 2024
1 parent 7fb9b64 commit b469b5b
Show file tree
Hide file tree
Showing 20 changed files with 29 additions and 134 deletions.
35 changes: 0 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ tiny_http = { version = "0.12", default-features = false }
tinystl = { version = "0.0.3", default-features = false }
tinyvec = { version = "1.6", features = ["alloc", "rustc_1_55"] }
tobj = "4.0"
tokio = { version = "1.24", default-features = false }
toml = { version = "0.8.10", default-features = false }
tracing = { version = "0.1", default-features = false }
tungstenite = { version = "0.20", default-features = false }
Expand Down
13 changes: 0 additions & 13 deletions crates/re_sdk/src/recording_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,6 @@ impl RecordingStreamBuilder {
/// Creates a new [`RecordingStream`] that is pre-configured to stream the data through to a
/// web-based Rerun viewer via WebSockets.
///
/// This method needs to be called in a context where a Tokio runtime is already running (see
/// example below).
///
/// If the `open_browser` argument is `true`, your default browser will be opened with a
/// connected web-viewer.
///
Expand All @@ -481,16 +478,6 @@ impl RecordingStreamBuilder {
/// ## Example
///
/// ```ignore
/// // Ensure we have a running tokio runtime.
/// let mut tokio_runtime = None;
/// let tokio_runtime_handle = if let Ok(handle) = tokio::runtime::Handle::try_current() {
/// handle
/// } else {
/// let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");
/// tokio_runtime.get_or_insert(rt).handle().clone()
/// };
/// let _tokio_runtime_guard = tokio_runtime_handle.enter();
///
/// let rec = re_sdk::RecordingStreamBuilder::new("rerun_example_app")
/// .serve("0.0.0.0",
/// Default::default(),
Expand Down
2 changes: 0 additions & 2 deletions crates/re_sdk/src/web_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ impl crate::sink::LogSink for WebViewerSink {
/// NOTE: you can not connect one `Session` to another.
///
/// This function returns immediately.
///
/// The caller needs to ensure that there is a `tokio` runtime running.
#[must_use = "the sink must be kept around to keep the servers running"]
pub fn new_sink(
open_browser: bool,
Expand Down
1 change: 0 additions & 1 deletion crates/rerun-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ rerun = { workspace = true, features = [

document-features.workspace = true
mimalloc.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }


[build-dependencies]
Expand Down
5 changes: 2 additions & 3 deletions crates/rerun-cli/src/bin/rerun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ use re_memory::AccountingAllocator;
static GLOBAL: AccountingAllocator<mimalloc::MiMalloc> =
AccountingAllocator::new(mimalloc::MiMalloc);

#[tokio::main]
async fn main() -> std::process::ExitCode {
fn main() -> std::process::ExitCode {
re_log::setup_logging();

let build_info = re_build_info::build_info!();

let result = rerun::run(build_info, rerun::CallSource::Cli, std::env::args()).await;
let result = rerun::run(build_info, rerun::CallSource::Cli, std::env::args());

match result {
Ok(exit_code) => std::process::ExitCode::from(exit_code),
Expand Down
9 changes: 1 addition & 8 deletions crates/rerun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ analytics = [
]

## Integration with `clap`.
clap = ["dep:clap", "dep:tokio"]
clap = ["dep:clap"]

## Support for using Rerun's data-loaders directly from the SDK.
##
Expand Down Expand Up @@ -84,7 +84,6 @@ run = [
"dep:re_log_encoding",
"dep:re_sdk_comms",
"dep:re_ws_comms",
"dep:tokio",
]

## Support for running a TCP server that listens to incoming log messages from a Rerun SDK.
Expand Down Expand Up @@ -143,12 +142,6 @@ rayon.workspace = true

# Native, optional:
clap = { workspace = true, optional = true, features = ["derive"] }
tokio = { workspace = true, optional = true, features = [
"macros",
"rt-multi-thread",
"time",
] }


[build-dependencies]
re_build_tools.workspace = true
43 changes: 16 additions & 27 deletions crates/rerun/src/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,24 @@ pub struct RerunArgs {
/// were passed in.
/// This object makes sure they live long enough and get polled as needed.
#[doc(hidden)]
#[derive(Default)]
pub struct ServeGuard {
tokio_rt: Option<tokio::runtime::Runtime>,
block_on_drop: bool,
}

// TODO:
impl Default for ServeGuard {
fn default() -> Self {
Self {
block_on_drop: false,
}
}
}

impl Drop for ServeGuard {
fn drop(&mut self) {
if let Some(tokio_rt) = self.tokio_rt.take() {
if self.block_on_drop {
eprintln!("Sleeping indefinitely while serving web viewer... Press ^C when done.");
tokio_rt.block_on(async {
tokio::time::sleep(std::time::Duration::from_secs(u64::MAX)).await;
});
std::thread::sleep(std::time::Duration::from_secs(u64::MAX));
}
}
}
Expand Down Expand Up @@ -129,27 +135,9 @@ impl RerunArgs {

#[cfg(feature = "web_viewer")]
RerunBehavior::Serve => {
let mut tokio_rt = None;

// Get the Tokio runtime for the current thread, or create one if there isn't any.
// If we do create one, we'll have to make sure it both outlives and gets
// polled to completion as we return from this method!
let tokio_rt_handle = if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle
} else {
tokio_rt
.get_or_insert(tokio::runtime::Runtime::new()?)
.handle()
.clone()
};

let server_memory_limit = re_memory::MemoryLimit::parse(&self.server_memory_limit)
.map_err(|err| anyhow::format_err!("Bad --server-memory-limit: {err}"))?;

// Creating the actual web sink and associated servers will require the current
// thread to be in a Tokio context.
let _tokio_rt_guard = tokio_rt_handle.enter();

let open_browser = true;
let rec = RecordingStreamBuilder::new(application_id).serve(
&self.bind,
Expand All @@ -159,9 +147,10 @@ impl RerunArgs {
open_browser,
)?;

// If we had to create a Tokio runtime from scratch, make sure it outlives this
// method and gets polled to completion.
let sleep_guard = ServeGuard { tokio_rt };
// Ensure the server stays alive until the end of the program.
let sleep_guard = ServeGuard {
block_on_drop: true,
};

Ok((rec, sleep_guard))
}
Expand Down
4 changes: 0 additions & 4 deletions crates/rerun/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ pub mod external {
#[cfg(not(target_arch = "wasm32"))]
pub use clap;

#[cfg(feature = "run")]
#[cfg(not(target_arch = "wasm32"))]
pub use tokio;

#[cfg(feature = "native_viewer")]
pub use re_viewer;

Expand Down
9 changes: 4 additions & 5 deletions crates/rerun/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl CallSource {
//
// It would be nice to use [`std::process::ExitCode`] here but
// then there's no good way to get back at the exit code from python
pub async fn run<I, T>(
pub fn run<I, T>(
build_info: re_build_info::BuildInfo,
call_source: CallSource,
args: I,
Expand Down Expand Up @@ -382,7 +382,7 @@ where
Command::Reset => re_viewer::reset_viewer_persistence(),
}
} else {
run_impl(build_info, call_source, args).await
run_impl(build_info, call_source, args)
};

match res {
Expand Down Expand Up @@ -594,7 +594,7 @@ fn profiler(args: &Args) -> re_tracing::Profiler {
profiler
}

async fn run_impl(
fn run_impl(
_build_info: re_build_info::BuildInfo,
call_source: CallSource,
args: Args,
Expand Down Expand Up @@ -720,7 +720,6 @@ async fn run_impl(
args.ws_server_port,
server_memory_limit,
)?;
let _ws_server_url = ws_server.server_url();

#[cfg(feature = "web_viewer")]
{
Expand All @@ -735,7 +734,7 @@ async fn run_impl(
args.web_viewer_port,
args.renderer,
open_browser,
&_ws_server_url,
&ws_server.server_url(),
)?
.block(); // dropping should stop the server
}
Expand Down
6 changes: 2 additions & 4 deletions examples/rust/custom_data_loader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
//! ```
use rerun::{
external::{anyhow, re_build_info, re_data_source, re_log, tokio},
external::{anyhow, re_build_info, re_data_source, re_log},
log::{DataRow, RowId},
EntityPath, TimePoint,
};

#[tokio::main]
async fn main() -> anyhow::Result<std::process::ExitCode> {
fn main() -> anyhow::Result<std::process::ExitCode> {
re_log::setup_logging();

re_data_source::register_custom_data_loader(HashLoader);

let build_info = re_build_info::build_info!();
rerun::run(build_info, rerun::CallSource::Cli, std::env::args())
.await
.map(std::process::ExitCode::from)
}

Expand Down
3 changes: 0 additions & 3 deletions examples/rust/custom_space_view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ re_sdk_comms = { path = "../../../crates/re_sdk_comms", features = ["server"] }

# mimalloc is a much faster allocator:
mimalloc = "0.1"

# We need tokio for re_sdk_comms:
tokio = { version = "1.24", features = ["macros", "rt-multi-thread"] }
3 changes: 1 addition & 2 deletions examples/rust/custom_space_view/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ mod color_coordinates_visualizer_system;
static GLOBAL: re_memory::AccountingAllocator<mimalloc::MiMalloc> =
re_memory::AccountingAllocator::new(mimalloc::MiMalloc);

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Direct calls using the `log` crate to stderr. Control with `RUST_LOG=debug` etc.
re_log::setup_logging();

Expand Down
6 changes: 2 additions & 4 deletions examples/rust/custom_store_subscriber/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@
use std::collections::BTreeMap;

use rerun::{
external::{anyhow, re_build_info, re_data_store, re_log, re_log_types::TimeRange, tokio},
external::{anyhow, re_build_info, re_data_store, re_log, re_log_types::TimeRange},
time::TimeInt,
ComponentName, EntityPath, StoreEvent, StoreId, StoreSubscriber, Timeline,
};

#[tokio::main]
async fn main() -> anyhow::Result<std::process::ExitCode> {
fn main() -> anyhow::Result<std::process::ExitCode> {
re_log::setup_logging();

let _handle = re_data_store::DataStore::register_subscriber(Box::<Orchestrator>::default());
// Could use the returned handle to get a reference to the view if needed.

let build_info = re_build_info::build_info!();
rerun::run(build_info, rerun::CallSource::Cli, std::env::args())
.await
.map(std::process::ExitCode::from)
}

Expand Down
3 changes: 0 additions & 3 deletions examples/rust/extend_viewer_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ re_sdk_comms = { path = "../../../crates/re_sdk_comms", features = ["server"] }

# mimalloc is a much faster allocator:
mimalloc = "0.1"

# We need tokio for re_sdk_comms:
tokio = { version = "1.24", features = ["macros", "rt-multi-thread"] }
3 changes: 1 addition & 2 deletions examples/rust/extend_viewer_ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use re_viewer::external::{
static GLOBAL: re_memory::AccountingAllocator<mimalloc::MiMalloc> =
re_memory::AccountingAllocator::new(mimalloc::MiMalloc);

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Direct calls using the `log` crate to stderr. Control with `RUST_LOG=debug` etc.
re_log::setup_logging();

Expand Down
1 change: 0 additions & 1 deletion examples/rust/minimal_serve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ publish = false

[dependencies]
rerun = { path = "../../../crates/rerun", features = ["web_viewer"] }
tokio = { version = "1.24", features = ["rt-multi-thread"] }
Loading

0 comments on commit b469b5b

Please sign in to comment.