Skip to content

Cannot create a PyWorker from python using Pyodide #1979

@sakhalifa

Description

@sakhalifa

Checklist

  • I added a descriptive title
  • I searched for other issues and couldn't find a solution or duplication
  • I already searched in Google and didn't find any good information or help

What happened?

Hello,

I am trying to adapt the worker-input example from polyscript to pyscript, but instead using Pyodide. According to the docs, I should be able to create a worker using a_worker = PyWorker(file). I tried a simpler example, a python script that just spawns a PyWorker that prints "a" to the console. I also used mini-coi to make web workers actually work.

Here is the structure of this test project

pyscript.json:

{
  "files": {
    "./a.py": "./a.py"
  }
}

index.html:

<!doctype html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css">
    <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script>
</head>
<body>
    <script type="py" config="./pyscript.json">
        from pyscript import PyWorker

        worker = PyWorker("./a.py")
    </script>
</body>
</html>

a.py:

print("a")

From the docs, it should work. Unfortunately, it does not, and instead throws an error in the console. What's weird is that it works with micropython. With the following index.html:

<!doctype html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css">
    <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script>
</head>
<body>
    <script type="micropython" config="./pyscript.json">
        from polyscript import XWorker

        worker = XWorker("./a.py")
    </script>
</body>
</html>

it actually prints "a" to the console, with no error.

It seems to be a problem with the modules from what i've read in the stacktrace.

What browsers are you seeing the problem on? (if applicable)

Firefox, Chrome

Console info

[pyscript/main] PyScript Ready

TypeError: s is undefined
    get blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
    Ft blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
    promise callback*Ft blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
    Ut blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
    <anonymous> blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
    value blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
    Gt blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
    <anonymous> blob:http://localhost:8080/7b3e6a6f-c03e-49c4-8f55-61b5e83a3406:2
error.js:15:15
    stderr error.js:15
    onerror core.js:112
    sn class.js:69
    (Asynchrone : EventListener.handleEvent)
    value main.js:67
    sn class.js:62
    l custom.js:89
    PyWorker core.js:111
    hiwire_call_bound pyodide.asm.js:9
    <anonyme> pyodide.asm.wasm:1421309
    <anonyme> pyodide.asm.wasm:1717908
    <anonyme> pyodide.asm.wasm:2532200
    <anonyme> pyodide.asm.wasm:2493051
    <anonyme> pyodide.asm.wasm:2481402
    <anonyme> pyodide.asm.wasm:1983178
    <anonyme> pyodide.asm.wasm:1717908
    <anonyme> pyodide.asm.wasm:2532200
    <anonyme> pyodide.asm.wasm:2548688
    <anonyme> pyodide.asm.wasm:1718788
    <anonyme> pyodide.asm.wasm:1717908
    <anonyme> pyodide.asm.wasm:1438814
    callPyObjectKwargs pyodide.asm.js:9
    callKwargs pyodide.asm.js:9
    runPython pyodide.asm.js:9
    Nt _python.js:12
    t utils.js:61
    nn/e[i]< hooks.js:65
    run utils.js:53
    onReady core.js:186
    queue custom.js:149
    (Asynchrone : promise callback)
    jn custom.js:146

Additional Context

Adding to it, it seems pyscript is the actual offender, as even using "mpy" as the script type does not fix it.

<!doctype html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2023.11.1/core.css">
    <script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script>
</head>
<body>
    <script type="mpy" config="./pyscript.json">
        from polyscript import XWorker

        worker = XWorker("./a.py")
    </script>
</body>
</html>

This does not work for the same reason as the original post.

Another addition, is that pyodide is actually not the one at fault here. Using this snippet :

<!doctype html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2023.11.1/core.css">
    <script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script>
</head>
<body>
    <script type="pyodide" config="./pyscript.json">
        from polyscript import XWorker

        worker = XWorker("./a.py")
    </script>
</body>
</html>

It actually works! This is quite a messy situation

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions