pyodide / pyodide

Pyodide is a Python distribution for the browser and Node.js based on WebAssembly
https://pyodide.org/en/stable/
Mozilla Public License 2.0
11.71k stars 789 forks source link

DOC Document how to disable Python to Javascript proxy #742

Open pckhoi opened 3 years ago

pckhoi commented 3 years ago

Hello, I'm running Pyodide in a web worker and whenever worker pass back result to the main thread and it happens to be a proxy, an error will be thrown because web worker can't serialize proxy. Right now this is what I do to handle it:

self.pyodide
    .runPythonAsync(python, () => {})
    .then(results => {
      // destroy proxy if one is passed
      if (results && results.destroy) {
        results.destroy();
        results = null;
      }
      self.postMessage({ results });
    })

It would be nice if there's a way to disable proxying altogether and avoid the (small) performance cost.

hoodmane commented 3 years ago

I think this is not a bug but a feature request. However, proxying cannot be disabled because it is used internally by pyodide type conversions, for instance any time a javascript function is called with a python object as an argument. This happens a lot.

The performance cost should also be small. I think the correct thing to do is add an FAQ item with this code as a suggestion.

hoodmane commented 2 years ago

We now have pyodide.isPyProxy which should be used instead.