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

Question about execution pause #768

Open
diamantisk opened this issue Oct 23, 2023 · 6 comments
Open

Question about execution pause #768

diamantisk opened this issue Oct 23, 2023 · 6 comments
Labels

Comments

@diamantisk
Copy link

Is there any way or workaround to pause the execution, store context and resume the execution and context from that point?

@emesare
Copy link
Member

emesare commented Oct 23, 2023

Yes, I think what you want is the Debugging Interface (enabled through the flag debugging).

@schungx
Copy link
Collaborator

schungx commented Oct 24, 2023

Is there any way or workaround to pause the execution, store context and resume the execution and context from that point?

Not exactly what you like to do. You have just described async, which Rhai is not.

However, as @emesare mentioned, you can use the debugging interface or use the on_progress callback. The pitfalls is that you usually need to use a dedicated thread to stop the execution and another thread to control it.

@diamantisk
Copy link
Author

diamantisk commented Oct 24, 2023

Thank you for pointing this out; I will certainly experiment with this idea. I envision a method where a host function, defined in Rust, is called by Rhai. This function would have the capability to suspend or yield the execution of Rhai at that specific point. After pausing and securely storing the context, an asynchronous task would be executed on the Rust side. Upon completion of this task, Rust would restore the previously stored context, allowing the Rhai script to resume execution seamlessly.

@schungx
Copy link
Collaborator

schungx commented Oct 24, 2023

You can probably do that easily by encapsulating a channel inside on_progress. Use a dedicated thread to run the engine, and use messages to control whether on_progress stalls, waiting for the resume command. There is no need to use the debugging interface if you just want simple start/stop. There is also no need to store any context, as the thread acts as the context.

@schungx
Copy link
Collaborator

schungx commented Oct 25, 2023

@schungx
Copy link
Collaborator

schungx commented Oct 25, 2023

I envision a method where a host function, defined in Rust, is called by Rhai

If you want such a suspend function, it is extremely simple to register one. Simply move in a pair of channels. When called, send a message to the tx channel, and wait for a reply from the rx channel. In the meantime, the thread suspends waiting for the channel message, which is exactly what you want to achieve.

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

No branches or pull requests

3 participants