tinkerbell/web/raylet.js
2020-12-04 23:13:06 -08:00

56 lines
1.8 KiB
JavaScript

languagePluginLoader.then(() => {
pyodide.loadPackage(["micropip", "cloudpickle", "numpy"]).then( () => {
pyodide.runPythonAsync(`
import micropip
import js
wheel_path = js.window.location.protocol + "//" + js.window.location.host + "/web/ray_web-0.0.1-py3-none-any.whl"
micropip.install(wheel_path)`)
.then(() => { pyodide.runPython(`
from ray import ray
ray.connect()
from ray.web import exec_work
`)
})
var wsprotocol = "ws:"
if (window.location.protocol == "https:") {
wsprotocol = "wss:"
}
var wspath = wsprotocol + "//" + window.location.host + "/api/ws"
function connect() {
var c = new WebSocket(wspath)
c.onopen = function() {
$("#status").text("Status: connected!")
c.send(JSON.stringify({
status: 2,
error_msg: "WebsocketWorker"
}))
}
c.onmessage = function(msg) {
var workText = workTerms[Math.floor(Math.random() * workTerms.length)];
$("#output").append("<p>" + workText + "...</p>")
pyodide.globals.torun = msg.data
pyodide.runPythonAsync("exec_work(torun)").then((res) => {
$("#output").append("<p>Did work! 👏</p>")
c.send(res)
})
};
c.onclose = function(e) {
$("#status").text("Status: disconnected. reconnecting...")
console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason);
setTimeout(function() {
connect();
}, 500);
};
c.onerror = function(err) {
console.error('Socket encountered error: ', err.message, 'Closing socket');
c.close();
};
};
connect();
}) })