emscripten-forge / recipes

Recipes to build the packages for the emscripten-forge distribution
BSD 3-Clause "New" or "Revised" License
55 stars 47 forks source link

Blocking IO requests in xeus-python #637

Closed martinRenou closed 6 months ago

martinRenou commented 1 year ago

@martinRenou I had another question that not relevant for this repo, but I can't think where to open an issue:

I want to adapt a library that performs blocking IO to work with emscripten. It's my understanding that it's impossible to perform blocking requests for byte ranges, because this hangs the same event loop that JS uses. Is that correct?

Originally posted by @agoose77 in https://github.com/emscripten-forge/recipes/issues/636#issuecomment-1716253974

martinRenou commented 1 year ago

blocking IO to work with emscripten. It's my understanding that it's impossible to perform blocking requests for byte ranges

I don't know how/if that would work. What would it look like in terms of code?

because this hangs the same event loop that JS uses

This is not correct, because the jupyterlite-xeus-python kernel runs in a web worker, so it does not hang the main thread thankfully. So you can make e.g. blocking XMLHttpRequest without freezing the page.

agoose77 commented 1 year ago

Ah yes, I forgot that the kernel acts in its own worker. That should help immensely. Now just to figure out CORS ;)

import pyjs

def request(method, url, body=None, headers={}):
    assert body is None

    js_request = pyjs.new(pyjs.js.XMLHttpRequest)
    js_request.open(method, url, False)

    for key, value in headers.items():
        js_request.setRequestHeader(key, pyjs.to_js(value))

    js_request.responseType = "arraybuffer"
    js_request.send(pyjs.to_js(None))

    response = pyjs.to_py(js_request.response)
    return memoryview(response)
DerThorsten commented 6 months ago

this is all not needed anymore, the request library should just work