update: Implement cancellation notification using mpsc::channel
This commit is contained in:
@@ -30,10 +30,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("🚀 Starting Worker...");
|
||||
println!("💡 Will cancel after 2 seconds\n");
|
||||
|
||||
// キャンセルトークンを先に取得(ロックを保持しない)
|
||||
let cancel_token = {
|
||||
// キャンセルSenderを先に取得(ロックを保持しない)
|
||||
let cancel_tx = {
|
||||
let w = worker.lock().await;
|
||||
w.cancellation_token().clone()
|
||||
w.cancel_sender()
|
||||
};
|
||||
|
||||
// タスク1: Workerを実行
|
||||
@@ -43,10 +43,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("📡 Sending request to LLM...");
|
||||
|
||||
match w.run("Tell me a very long story about a brave knight. Make it as detailed as possible with many paragraphs.").await {
|
||||
Ok(WorkerResult::Finished(_)) => {
|
||||
Ok(WorkerResult::Finished) => {
|
||||
println!("✅ Task completed normally");
|
||||
}
|
||||
Ok(WorkerResult::Paused(_)) => {
|
||||
Ok(WorkerResult::Paused) => {
|
||||
println!("⏸️ Task paused");
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -59,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
println!("\n🛑 Cancelling worker...");
|
||||
cancel_token.cancel();
|
||||
let _ = cancel_tx.send(()).await;
|
||||
});
|
||||
|
||||
// タスク完了を待つ
|
||||
|
||||
Reference in New Issue
Block a user