Hi all,
I have a small technical question related to the use of GLib. I need to use an asynchronous piece of code within a glib::closure
.
The problem can be illustrated with the following code (which does not compile):
signaller.connect_closure(
"session-requested",
false,
glib::closure!(
|_signaller: glib::Object,
session_id: &str,
peer_id: &str,
offer: Option<&gstwebrtc::WebRTCSessionDescription>| {
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
}
),
);
Something like this:
signaller.connect_closure(
"session-requested",
false,
glib::closure!(
|_signaller: glib::Object,
session_id: &str,
peer_id: &str,
offer: Option<&gstwebrtc::WebRTCSessionDescription>| {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async {
println!("Waiting...");
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
println!("Waiting finished");
});
}
),
);
crashes with the error:
Cannot start a runtime from within a runtime. This happens because a function (like
block_on
) attempted to block the current thread while the thread is being used to drive asynchronous tasks.
This suggests that Tokio is already set up somewhere. I’ve tried something like:
let context = glib::MainContext::default();
context.block_on(async {
println!("Waiting...");
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
println!("Waiting finished");
});
But it blocks forever, and I’m not exactly sure of what I’m doing. Any suggestions?
Thanks!