callMain() multiple times in web worker

Hi, I am currently running Python in a web worker. I use the following code to run a Python script.

callMain(["-c", "print('hello')"])

Now I’m trying to reuse my web worker so that I can run another Python code snippet. Using callMain again causes errors bacause Python is already initialized.

Is there a way of refreshing the web worker’s global state so that I can run multiple code snippets, one after the other?

At PyCon I discussed a similar issue with Pyodide maintainer Hood Chatham and he recommended terminating the worker and making a new one.

I ended up with code similar to this:

// Reset the worker
pythonWorker = makeWorker();

Where makeWorker was the function that actually created the worker:

function makeWorker() {
    const worker = new Worker('/worker.js');
        type: 'initialize',
    return worker;