This looks like it's exactly what i need for a project im working on. Right now Im actually starting a thread and raising an exception to stop it and restart which is not very safe.
If its not a secret, do you mind sharing what the use case is? I'm curious because my thoughts originally were that this would be useful when running something that takes more than the trivial few minutes to finish.
So I'm actually doing web browser automation via llm agents. So prompting to browser automation like "go to this webpage and do this". I have an algorithm for classifying html and then i'm generating "behaviors" to interact with it for each bot. The behaviors the llm generates are actually in a superset language that i parse the ASTs with lark. In the lark parsing the behaviors (like web scraping or element interactions) need to be stopped if the user says to do something else or stop or restart them...and man it's been a pain getting them to stop and clean up in a memory efficient way. Right now starting asyncio coroutines and canceling them are the best way ive found since a lot of this is async (using playwright)
Wow that’s an actual use case! Pretty cool stuff. Unfortunately I think getting this code to run with asyncio would be a lot more work since there’s a lot more to consider. I guess it also depends on how stateful the coroutines are. The less state the easier it should be to use my jump method to do what you want.
haha yea i've been really enjoying this project. In terms of statefulness. It is quite a stateful coroutine. I will play around with it and see if i can get it working. Would be nice to use a more goto like method than raising exceptions. Thanks for your work