-
Notifications
You must be signed in to change notification settings - Fork 74
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
Mixing runtimes #96
Comments
Hmmm, can you throw together a repo or test case? It's probably a straightforward (as you say) case, but I've run (I was building a magic-wormhole for macOS app before I got... sidetracked... by life, lol: https://twitter.com/ryanmcgrath/status/1657967789142069249) |
Most likely it's my inexperience with Rust runtimes (including Tokio), but here's a simple example of Tokio functioning normally, but nothing happening from AppKit: use std::time::Duration;
use cacao::appkit::{App, AppDelegate};
struct MacApp;
impl AppDelegate for MacApp {
fn did_finish_launching(&self) {
println!("AppKit running");
}
}
#[tokio::main]
async fn main() {
tokio::task::spawn(async {
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
println!("Timer");
}
});
println!("Starting AppKit");
tokio::task::spawn_blocking(|| {
App::new("com.example.cacao_runtime", MacApp).run();
});
println!("After AppKit");
// We await this to keep the runtime alive/in scope
let _ = tokio::task::spawn_blocking(|| {
let mut spin_count: u128 = 0;
loop {
if spin_count % 100_000_000 == 0 {
println!("Tick");
}
spin_count += 1;
}
})
.await;
} [package]
name = "cacao_runtime"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cacao = "0.3.2"
tokio = { version = "1.29.1", features = ["rt-multi-thread", "macros", "time"] } Expected output would be something like:
but what we actually get is:
That is, just missing the |
I neglected to check Console (it's been a while since I've done native Mac dev) and it has the errors I would expect to see:
I'm not horribly surprised at this, but I am curious how it knows that it is not running in the main thread (is it grabbing the process's thread list and ordering them, instead of just marking the thread it started on as main?). I'm guessing the trivial solution is to start the Tokio runtime from within AppKit's, which would be more traditional for Apple apps. |
Yeah, so what I've done is just start a new thread inside of The Elm-style of messaging is still optimal for running the UI flow in cacao, so just message pass back when things are done. I can try to clean up and open source my example code for this if it'd be helpful. |
I'm interested in how others may have mixed the AppKit runtime with something Rust-centric, whether
async-std
,tokio
, or something else. Use cases are primarily in mixed platform systems, the core Rust code can't be designed around the standard Apple runtime, and needs something from the Rust ecosystem to actually be usable.I have found that naively calling
NSApplication.run()
withintokio::task::spawn_blocking
results in the event loop hanging.The text was updated successfully, but these errors were encountered: