Skip to content

Commit 5d2289f

Browse files
docs: update example to demonstrate Backend enum usage [7/7]
Update the blocking_genserver example to demonstrate the new Backend enum: - Import Backend from tasks module - Use start_with_backend(Backend::Thread) for blocking actors - Use start_with_backend(Backend::Async) for async actors - Add detailed documentation explaining when to use each backend - Improve code comments explaining the blocking actor problem 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9abf916 commit 5d2289f

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

examples/blocking_genserver/main.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::time::Duration;
33
use std::{process::exit, thread};
44

55
use spawned_concurrency::tasks::{
6-
RequestResult, MessageResult, Actor, ActorRef, send_after,
6+
RequestResult, MessageResult, Actor, ActorRef, send_after, Backend,
77
};
88

99
// We test a scenario with a badly behaved task
@@ -93,16 +93,30 @@ impl Actor for WellBehavedTask {
9393
}
9494
}
9595

96-
/// Example of start_blocking to fix issues #8 https://github.com/lambdaclass/spawned/issues/8
97-
/// Tasks that block can block the entire tokio runtime (and other cooperative multitasking models)
98-
/// To fix this we implement start_blocking, which under the hood launches a new thread to deal with the issue
96+
/// Example demonstrating Backend selection for Actor execution.
97+
///
98+
/// This example shows how to use `start_with_backend()` to choose the right
99+
/// execution context for different types of actors:
100+
///
101+
/// - `Backend::Thread`: For blocking actors that would freeze the async runtime
102+
/// - `Backend::Async`: For well-behaved async actors (the default)
103+
///
104+
/// The problem: Tasks that block can freeze the entire tokio runtime.
105+
/// The solution: Use `start_with_backend(Backend::Thread)` for blocking actors.
106+
///
107+
/// See also: https://github.com/lambdaclass/spawned/issues/8
99108
pub fn main() {
100109
rt::run(async move {
101-
// If we change BadlyBehavedTask to start instead, it can stop the entire program
102-
let mut badboy = BadlyBehavedTask::new().start_on_thread();
110+
// A blocking actor that would freeze the runtime if started with Backend::Async
111+
// Using Backend::Thread gives it a dedicated OS thread where blocking is safe
112+
let mut badboy = BadlyBehavedTask::new().start_with_backend(Backend::Thread);
103113
let _ = badboy.cast(()).await;
104-
let mut goodboy = WellBehavedTask::new(0).start();
114+
115+
// A well-behaved async actor can use the default Backend::Async
116+
// This is equivalent to calling .start()
117+
let mut goodboy = WellBehavedTask::new(0).start_with_backend(Backend::Async);
105118
let _ = goodboy.cast(()).await;
119+
106120
rt::sleep(Duration::from_secs(1)).await;
107121
let count = goodboy.call(InMessage::GetCount).await.unwrap();
108122

0 commit comments

Comments
 (0)