Settings

Theme

Show HN: Python Code Resumption. A way to stop and resume a Python script

github.com

4 points by ag_rin · 5 comments · 1 min read

Reader

Wanted to share this crazy little idea I had and implemented. Not sure if anyone has tried to do this in python before, but I wanted a way to interrupt a python script and then restart it from the interrupt point later.

Obviously the implementation is very POC and simple, but it works. Let me know your thoughts! What crazy python stuff have you done?

1 thread
jawerty

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.

  • ag_rinOP

    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.

    • jawerty

      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)

      • ag_rinOP

        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.

        • jawerty

          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

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection