async: Task.scope/Task.start over asyncio.TaskGroup, a trailing unit expression ends a block, and the mailbox agent in the cookbook - #115
Merged
Conversation
…expression ends a block, and the mailbox agent in the cookbook (#108, #109) Structured concurrency as a library: Task.scope runs an async body inside async with asyncio.TaskGroup() as tg (a new PyStmt::AsyncWith IR node) and hands it the opaque Scope; Task.start needs that Scope, so a start outside a scope is a missing argument at compile time. No task { } spelling: a user builder cannot open the scope around the whole block and the library form reads well enough. A trailing unit expression now ends a result/option/async block with () (F#'s implicit Zero). The agent lives in examples/interop/structured_concurrency.pyfun as an asyncio.Queue behind two externs and a recursive async loop over a Msg ADT, started inside a scope so it dies with the session. Lesson 18 gets a structured-concurrency section; DESIGN §6 and §8.1, the cookbook README and ROADMAP items 17 and 18 are updated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #109 and the
Agenthalf of #108 (as a cookbook example, per ROADMAP item 18).Structured concurrency, the library form
Every task is owned by a scope, the scope does not exit until its children finish, one failure cancels the siblings, and leaving the scope cancels everything. Two prelude members type the discipline over
asyncio.TaskGroup:Task.scope(Scope ->{e} Async a) ->{e} Async aTask.startScope -> Async unit ->{io} unitScopeis an opaque built-in handle onlyTask.scopehands out, so a start outside a scope is a missing argument at compile time (expected Scope, found Async unit), and there is nocancelto forget. The Python IR gainsPyStmt::AsyncWith; the helpers are:Decided: no
task { }spelling. The user-builder route was tried on paper first, as agreed: a builder cannot open the scope around the whole block because the protocol has norunmember, andTask.scope (fun scope -> async { … })reads well enough that addingrunwaits for demand. §8.1 stays at four built-ins, untouched. TheExceptionGrouptyping (Result a (List Exception)) stays open until a program needs the members; todayAsync.catchreports it as oneException. All of this is inDESIGN.md§6.A trailing unit expression ends a block
The cookbook producer ends in
post inbox Quit, a plain call, and F#'s implicitZeromakes that the block's end. A trailing unit expression (parsed aslet _ = e) now ends aresult/option/asyncblock with():asyncfalls off theasync def,result/optionemitreturn Ok(None)/Some(None). The checker pins the last step tounit.The mailbox agent (#108)
examples/interop/structured_concurrency.pyfun: anasyncio.Queuebehind two externs (post=.put_nowait,take=.get()typedAsync a), a recursiveasyncloop that is amatchover aMsgADT, and a producer task started withTask.startinside aTask.scope, so the agent dies with the session. It printsagent heard hello,agent heard world,agent handled 2 messages, and therun_exampleharness pins that. Lesson 18 gets a structured-concurrency section with a two-worker example; the cookbook README gets the row.Tests
Typecheck: the scope/start types, the missing-
Scopeerror,let purerejected overTask.start, and the trailing unit expression in all three monads (withoption { print 1 } : Option uniton hover). E2E: the cookbook example, and the trailing-expression lowering inresult/optionincluding the short-circuit path.cargo test,cargo clippy --all-targets,cargo fmt --checkanddocs/verify_lessons.pyare clean.