Code Monkey home page Code Monkey logo

unthrow's Introduction

Unthrow package for python 3.8

This defines a function stop(message), which saves the stack state and then throws an unthrow.ResumableException.

ResumableException has a method run_once() which runs a main loop function, and if it was previously stopped, restores the stack state to where stop was called. It should work with loops, with statements and other block things. It may or may not work with call stacks that include C code...

This allows you to stop and start the interpreter from deep in the stack, to do stuff in javascript or whatever is running the interpreter..

3.9 stores stack level as a number not a pointer, it will need this minor change to work on 3.9

To make it work:

  1. Create a resumer object (at the level you want interrupts and the like to happen).
  2. If you want regular interrupts as well as deliberately thrown ones, then call resumer.set_interrupt_frequency(line_count), which throws an interrupt every line_count lines of python execution.
  3. Then just repeatedly call run_once with your main function (the thing you want to be resuming from) until it is finished (resumer.finished==True). It will set finished when the main function returns normally (as opposed to an interrupt or a call to unthrow.stop making it fall out.
import unthrow
# this is an arbitrary argument that will
# be passed to main application, e.g. a state object
__appArg="WOO"
r=unthrow.Resumer()
# throw an interrupt every 2 lines of execution
r.set_interrupt_frequency(2)
while r.finished==False:
    r.run_once(mainApp,__appArg)
    # do anything that is supposed to happen outside the main loop here
    # like handle input or whatever

If you're using it in pyodide and you want interrupts to drop right out to javascript, you can call run_once from javascript and use settimeout to allow you to handle js messages or whatever. Something roughly like this:

function init()
{
  pyodide.runPython(`
__appArg="WOO" 
import unthrow
__resumer=unthrow.Resumer()`)
# interrupt and handle js stuff every 50 lines of code
r.set_interrupt_frequency(50);
}

function doLoop()
{
  pyodide.runPython(`
__resumer.run_once(mainApp, __appArg)
`)
  setTimeout(doLoop,0);
}

In your main loop, where you want to drop out in a resumable way, you can call unthrow.stop, with a parameter which will turn up in the resumer object's resume_params member.

e.g. My time.sleep implementation looks like this:

import time
time.sleep=lambda t: unthrow.stop({"cmd":"sleep","time":t})

Then in the javascript, it looks like this; i.e. time.sleep throws an unthrowable exception, which then goes back out to javascript which does a settimeout before the next loop iteration runs:

function doLoop()
{
  let retVal=pyodide.runPython(`__resumer.run_once(mainApp, __appArg)`)
  let result=retVal.toJs();
  if(result.cmd && result.cmd=='sleep')
  {
    setTimeout(doLoop,result.time*1000);
  }else
  {
    setTimeout(doLoop,0);
  }
}

unthrow's People

Contributors

joemarshall avatar fishbone avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.