-
Notifications
You must be signed in to change notification settings - Fork 181
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
How do I checkpoint and resume Rhai evaluation? #769
Comments
What you want is called async, which Rhai isn't. Async means being able to suspend operation in the middle, store the context (whatever), and then resume later on. You can easily do that by running the |
The example does not seem to prevent many paused executions from hogging as many OS threads. Pausing from Callbacks / closures seem to work as a makeshift suspension points. But they lead to ugly Rhai source code. What can be done with a patterns like this:
, so it would look like
Can Rhai's "custom syntax" be used for that? Or maybe better to just pre-process source code and just turn |
Well, that's the pitfalls of using raw threads. Async is invented just to solve your problem. The compiler saves up you calling stack, allowing you to resume later on. What you described is actually "coroutines" which forms the basis of async in modern languages. Unfortunately Rhai is not intended for async use. If you need it, the you can look into an async scripting engine. IMHO, it is a very bad idea to do async in scripts (look at the mess called JavaScript). If you intend scripts to be written by non-expert programmers (otherwise why use scripts?) then you don't really want users to hassle with the problem of async. Rhai is not intended for this usage scenario. You are better advised to split your API into sections with the async barriers in between. If you want to script an entire application in Rhai with an async API, just like JavaScript... then maybe using JavaScript is best for your use case. In your example, you can separate your API into different functions: fn on_init() {
print(1);
}
fn on_suspend() {
return 42;
}
fn on_resume(x) {
print(x);
}
fn on_stop() {
print(4);
} |
What you described is, again, async. This is called the CPS (Continuation-Passing Style) of an async call, before the popular JavaScript has massive amounts of CPS code which led to callback-hell. |
I want something like this
It should print
1\n2\n3\n4\n
.Is there some example (e.g. using
internals
feature) to attain this?Alternatively, is there something like "call with current continuation" in Rhai, to avoid each suspension point becoming a closure in Rhai code (i.e. callback hell)?
The text was updated successfully, but these errors were encountered: