google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use context for runtime, and allow to control timeout for script running?

Solveay opened this issue · comments

commented

You can already achieve this with the current API, but it requires an additional goroutine to transmit cancellation of ctx to the interpreter, something like this:

{
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	go func() {
		<-ctx.Done()
		thread.Cancel("context cancelled")
	}()
	... evaluate starlark in 'thread' ...
}

Perhaps we could make this simpler by adding a convenience function to the API, or maybe all it needs is an executable example of cancellation in action.

If you pass the context in the thread using Thread.SetLocal, then long-running built-in functions can retrieve it and honor cancellation too; see #252 (comment).

See also: