Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for async tasks #277

Open
irh opened this issue Jan 22, 2024 · 1 comment
Open

Add support for async tasks #277

irh opened this issue Jan 22, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@irh
Copy link
Contributor

irh commented Jan 22, 2024

It would be good to have async functions in Koto, and I think that it would be a natural extension of the existing Generator concept to add support for async workflows.

Generator functions work by creating an iterator when first called, which contains a VM that's responsible for executing the function, suspending execution each time yield is encountered. Each time .next() is called the iterator runs until the next yield expression, or the function exits.

I think generators could be replaced with a new Task value type that supports an await expression alongside the existing yield.

await would suspend execution of the task while waiting for output from some other task. The task's VM would either block on the awaited result becoming available, or it could be managed by an async runtime that monitors the progress of multiple parallel tasks.

So, the new Task type will be able to:

  • be suspended while waiting for the output of another task (with await).
  • yield a series of values (by declaring itself as iterable to the runtime).
  • be used in a blocking context (like when a generator is wrapped in an iterator).
  • be used in a non-blocking context (like in an async runtime).
@irh irh added the enhancement New feature or request label Jan 22, 2024
@irh
Copy link
Contributor Author

irh commented Mar 1, 2024

Some groundwork for this was implemented in #291 - the VM now has an execution_state field which has a Suspended variant, used when a generator has just yielded a value.

Additionally, continue_running now returns a ReturnOrYield enum that lets the caller know the nature of the resulting value.

ReturnOrYield could be extended (and renamed) to include an Await variant that passes the task that's being waited on to an executor (or something 🙃).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant