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

Docs, examples, explanation? #1

Open
alexmojaki opened this issue Apr 21, 2021 · 8 comments
Open

Docs, examples, explanation? #1

alexmojaki opened this issue Apr 21, 2021 · 8 comments

Comments

@alexmojaki
Copy link

Like other people in pyodide/pyodide#1219 I'm trying to port an educational platform (http://futurecoder.io/) to pyodide and want to get input() working nicely. So I'm trying to understand this code to see if it's a solution. It looks very cool, but I don't get it.

The README definitely needs updating, since ResumableException.resume() doesn't exist.

console_async.html doesn't work when I run it because it can't find pyodide.js and I don't know what I'm supposed to do about that. I also can't understand what's going on just by looking at the code.

Can you provide a small example Python script with threads or something that shows stopping and resuming? I can run test.py but I can't see what to do with it.

Am I right that this is incompatible with sys.settrace()? Part of the motivation for all this is debuggers like pdb (pyodide/pyodide#550) and in my use case there's also another debugger that uses settrace but no interaction with stdin. Seems that when I start pdb the interrupts stop because I don't see TP printed any more.

@joemarshall
Copy link
Owner

Hey,

I'm currently snowed under with teaching, but check out:

https://joemarshall.github.io/websensors/

for an example of how I'm using it. Source code link at the bottom of each page. The useful stuff lives in assets/js/async_worker.js

@joemarshall
Copy link
Owner

About pdb, right now we use a c trace function to do resuming and interrupts, and I don't do anything clever with any existing trace functions so I suspect bad things will happen with pdb. There's actually some Python patches in pyodide now which might in theory enable interrupts without doing trace. Or one could override sys.settrace to make sure our trace function didn't get killed and calls through to the pdb one.

One other thing it is worth knowing by the way is that you can't use web workers in Safari right now. They have a very limited stack depth which means pyodide can't run. That's why my code uses a fake web worker now. I have spent quite a lot of time swearing at Apple recently

@joemarshall
Copy link
Owner

It occurs to me, if you're willing to restrict yourself to browsers where webworkers work, then you don't really need interrupts for most stuff, you can kill the webworker to stop an infinite loop. Implementing input would be easy enough, you just need to make it call unthrow.stop, which gets you out to js then set some JavaScript variable with the result before resuming.

Pdb still won't work until the trace function thing is fixed though.

@alexmojaki
Copy link
Author

There's actually some Python patches in pyodide now which might in theory enable interrupts without doing trace

Yeah it sounds like that's the plan here but I'm not really sure. Sounds like hoodmane is integrating the concept behind your code directly into the pyodide interpreter so users wouldn't actually call unthrow.

you just need to make it call unthrow.stop, which gets you out to js then set some JavaScript variable with the result before resuming.

The point of this issue is that I still don't understand what stopping and resuming looks like in Python code since there's no 'resume' function any more. But as I said the question seems moot now.

One other thing it is worth knowing by the way is that you can't use web workers in Safari right now. They have a very limited stack depth which means pyodide can't run. That's why my code uses a fake web worker now. I have spent quite a lot of time swearing at Apple recently

Oh wow that's ridiculous, thanks for letting me know.

So you're doing everything in the browser thread and trying to use unthrow to prevent it freezing up? Do you have it working? I tried that page you linked and once I use input() or an infinite loop it stops working sensibly.

@joemarshall
Copy link
Owner

On https://joemarshall.github.io/websensors/lessons/1.Introduction_to_Web_Python/python_intro1.html

if I put

while True:
    print("WOO")

or

while True: pass

into a code box and click start, I can stop the browser. It doesn't respond super quick because it is running 50 lines or something before it does it, but it does respond. There's no input implementation on there though, the interrupts are just used for

  1. sensor data receiving.
  2. time.sleep
  3. Making it so stop and start works despite infinite code loops.

I think it is probably possible to make an uninterruptible loop, but I can't work out how... The important thing my end is that time.sleep works, so they can adjust frequency easily.

If you do something in a with call, that can break things, because with is not interruptible (because cleanup of the with is automatically called on exception)

@alexmojaki
Copy link
Author

Weird, when I tried that before the page froze up, but I can't reproduce it now.

@joemarshall
Copy link
Owner

Phew. Thought I was about to get a million emails from students after the last iOS fix!

@joemarshall
Copy link
Owner

Also, I added some up to date readme in case that is useful.

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

No branches or pull requests

2 participants