Skip to content

add async package#7

Open
allwefantasy wants to merge 2 commits intomainfrom
hailin/add_async
Open

add async package#7
allwefantasy wants to merge 2 commits intomainfrom
hailin/add_async

Conversation

@allwefantasy
Copy link
Copy Markdown

No description provided.

@bobzhang bobzhang requested a review from Guest0x0 October 9, 2025 06:49
"name": "your-project-name",
"version": "0.1.0",
"deps": {
"moonbitlang/async": "0.8.0"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too old. Can this be updated automatically?

}
```

**Key Change**: Now use `async fn main` directly, no longer need `@async.with_event_loop` wrapper.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary?


```moonbit
///|
async fn timeout_examples() -> Unit {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe introduce and use async test?

}
```

## Structured Concurrency and Task Groups
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need some precise textual description of the semantic here. Maybe just copy-and-paste the relevant parts from the docstring of with_task_group & spawn_bg?

defer file.close()

// Write data
file.write_string("Line 1\nLine 2\nLine 3\n",encoding=@io.UTF8)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already deprecated

async fn concurrency_patterns() -> Unit {
// Limit concurrency
@async.with_task_group(fn(group) {
let semaphore = @async.Queue::new()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we now have native moonbitlang/async/semaphore

for i in 0..<20 {
group.spawn_bg(fn() {
let _permit = semaphore.get() // Acquire permit
defer semaphore.put(()) // Release permit
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should acquire outside spawn

// Task group ensures all tasks complete
})
}
```
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we enforce proper task lifecycle management via with_task_group, so this section does not make a lot of sense

```moonbit
///|
async fn logging_example() -> Unit {
@pipe.stderr.write("Starting request processing\n")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, outdated

1. **Use `inspect()` for Assertions**: MoonBit's `inspect()` function is perfect for testing async results
2. **Test Timing Behavior**: Use `@async.sleep()` to simulate realistic timing scenarios
3. **Test Error Conditions**: Always test timeout, cancellation, and error propagation
4. **Use StringBuilder for Logging**: Capture execution order and verify concurrent behavior
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that we have a native @test.Test type for this, which should be more idiomatic. Maybe just remove this one?

@Guest0x0
Copy link
Copy Markdown

Guest0x0 commented Nov 7, 2025

Since the async package is still changing a lot, I think one option to keep things up-to-date is:

  • hand-written document for task group and a simple introduction to each package in moonbitlang/async
  • copy-and-paste README.mbt.md for important packages (@fs, @http, @process)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants