-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Load Pyodide runtime from external capnproto file #2430
Conversation
9a69cc8
to
0f5e39a
Compare
0f5e39a
to
50a1faf
Compare
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
src/workerd/api/pyodide/pyodide.c++
Outdated
kj::Maybe<kj::Array<unsigned char>> pyodideBundleDataGlobal = kj::none; | ||
kj::Maybe<kj::Own<capnp::FlatArrayMessageReader>> pyodideBundleReaderGlobal = kj::none; | ||
kj::Maybe<jsg::Bundle::Reader> pyodideBundleGlobal = kj::none; | ||
|
||
void setPyodideBundleData(kj::Array<unsigned char> data) { | ||
pyodideBundleReaderGlobal = kj::heap<capnp::FlatArrayMessageReader>(kj::arrayPtr( | ||
reinterpret_cast<const capnp::word*>(data.begin()), data.size() / sizeof(capnp::word))); | ||
pyodideBundleGlobal = KJ_REQUIRE_NONNULL(pyodideBundleReaderGlobal)->getRoot<jsg::Bundle>(); | ||
pyodideBundleDataGlobal = kj::mv(data); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel too strongly about this, but I think it might be worth to reduce the number of globals here. Creating a reader should be fast, so maybe you could instead only keep the pyodideBundleDataGlobal
global and create the jsg::Bundle::Reader
on-demand?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the ownership semantics here, I'm concerned that the jsg::Bundle::Reader
doesn't own its data and instead the data is owned by one of pyodideBundleReaderGlobal
or pyodideBundleDataGlobal
, so if I let either of those go out of scope while the ModuleRegistry
object is still around, then when a file is imported, we'll read from freed data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at other examples of capnp::FlatArrayMessageReader
it seems it is important to keep it all alive. But it looks like you can reduce the number of globals at least a little bit by using attach
: https://github.com/cloudflare/workerd/blob/main/src/workerd/server/workerd.c%2B%2B#L991
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I am planning to eventually move these into a TreeMap
or something, but it would be nice not to worry about manually keeping the other stuff alive.
src/workerd/server/workerd-api.c++
Outdated
@@ -446,7 +446,9 @@ void WorkerdApi::compileModules( | |||
if (hasPythonModules(confModules)) { | |||
KJ_REQUIRE(featureFlags.getPythonWorkers(), | |||
"The python_workers compatibility flag is required to use Python."); | |||
// Inject pyodide bootstrap module. | |||
// Inject Pyodide bundle | |||
modules->addBuiltinBundle(PYODIDE_BUNDLE, kj::none); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PYODIDE_BUNDLE
will get replaced with pyodideBundleGlobal
here, right? Maybe add a todo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a todo, I guess @garrettgu10 is working on it =)
50a1faf
to
82f3fd5
Compare
These changes are needed to load the Pyodide runtime from an external capnproto file. In workerd, we'll keep loading Pyodide in the same way, but we moved this to WorkerdAPI. In our internal codebase, we now download the bundle and call `setPyodideBundleData` when we load the first Python worker. Then when setting up the module registry, we add the bundle from `pyodideBundleGlobal`. TODO in followups: 1. Make workerd load the bundle in the same way 2. Add support for multiple versions of the bundle
82f3fd5
to
8fcd672
Compare
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
This adds a github action that makes a binary file out of the pyodide capnproto bundle and uploads it to GCS. TODO: 1. Use the uploaded file (see #2430) 2. Move the pyodide js code and this script to pyodide-build-scripts repo.
These changes are needed to load the Pyodide runtime from an external capnproto file. In workerd, we'll keep loading Pyodide in the same way, but we moved this to WorkerdAPI. In our internal codebase, we now download the bundle and call
setPyodideBundleData
when we load the first Python worker. Then when setting up the module registry, we add the bundle frompyodideBundleGlobal
.TODO in followups: