lean-ja / lean-ja/lean-by-example
IOでタイムアウト付き処理をする
Open
Nobody has claimed this yet.
コード例
- Dominant language
- Lean
- Stars
- 188
- Forks
- 15
- Avg merge
- 9h 8m
- Merged PRs (30d)
- 6
Description
I have used the following code elsewhere:
import Lean
open Lean
/--
Return type used internally by `withTimeout`.
-/
inductive TimeoutResult (α : Type) where
| success (val : α)
| timeout
/--
Run a computation with a timeout.
-/
def withTimeout (timeout : UInt32) (x : IO α) : IO α := do
let timeoutTask ← IO.asTask <| IO.sleep timeout >>= fun _ => return TimeoutResult.timeout
let mainTask ← IO.asTask (prio := .dedicated) <| TimeoutResult.success <$> x
match ← IO.waitAny [mainTask, timeoutTask] with
| .ok <| .success a =>
IO.cancel timeoutTask
return a
| .ok <| .timeout =>
IO.cancel mainTask
throw <| .userError s!"Operation timed out after {timeout}ms"
| .error e =>
IO.cancel mainTask
IO.cancel timeoutTask
throw e
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides a Lean IO timeout snippet but names no repository file, test, or entry point. First inspect the repository's existing IO examples and issue conventions to determine where this example belongs; done should be a clearly placed, working timeout example with the intended behavior documented.
Written by the indexing model from the issue text.
Assessment
- Domain
- documentation
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100