Skip to content

Commit

Permalink
syncFS after uncertainty_update or fit final, and send autosave inter…
Browse files Browse the repository at this point in the history
…val to fit worker
  • Loading branch information
bmaranville committed Jun 10, 2024
1 parent 900c772 commit 1621d50
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions build_standalone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
HERE=$(pwd)
BUMPS_DIR=/Users/bbm/dev/bumps
REFL1D_DIR=/Users/bbm/dev/refl1d
MOLGROUPS_DIR=/Users/bbm/dev/molgroups
TARGET=$HERE/public/wheels
export BUILD_EXTENSION=True

Expand All @@ -11,9 +12,12 @@ export BUMPS_WHEEL_FILE=$(ls dist)
#cd $REFL1D_DIR && python setup.py bdist_wheel
cd $REFL1D_DIR && pyodide build
export REFL1D_WHEEL_FILE=$(ls dist)
cd $MOLGROUPS_DIR && python setup.py bdist_wheel
export MOLGROUPS_WHEEL_FILE=$(ls dist)

cd $HERE
npm run build -- --sourcemap
mkdir -p $TARGET
cp $BUMPS_DIR/dist/*.whl $TARGET
cp $REFL1D_DIR/dist/*.whl $TARGET
cp $MOLGROUPS_DIR/dist/*.whl $TARGET
11 changes: 8 additions & 3 deletions src/standalone_fit_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function loadPyodideAndPackages() { // loads pyodide
api.FitThread.join = lambda self, timeout: None
wrapped_api = {}
def expose(method, method_name):
def wrapper(args):
pyargs = args.to_py() if args is not None else []
Expand All @@ -59,10 +59,15 @@ async function loadPyodideAndPackages() { // loads pyodide
if method_name in ["start_fit_thread"]:
wrapped_api[method_name] = expose(method, method_name)
def set_autosave_session_interval(interval: int):
print("new interval", interval, type(interval))
api.state.shared.autosave_session_interval = interval
def set_problem(dilled_problem):
problem = dill.loads(dilled_problem)
api.state.problem.fitProblem = problem
wrapped_api["set_autosave_session_interval"] = expose(set_autosave_session_interval, "set_autosave_session_interval")
wrapped_api["set_problem"] = expose(set_problem, "set_problem")
def fit_progress_handler(event):
Expand All @@ -73,9 +78,9 @@ async function loadPyodideAndPackages() { // loads pyodide
api.EVT_FIT_PROGRESS.connect(fit_progress_handler, weak=True)
api.EVT_FIT_COMPLETE.connect(fit_complete_handler, weak=True)
wrapped_api
`);
`);
return api;
}

Expand Down
27 changes: 20 additions & 7 deletions src/standalone_worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import { loadPyodideAndPackages } from './pyodide_worker.mjs';
import { expose, wrap, proxy } from 'comlink';
import { expose, wrap, proxy, Remote } from 'comlink';
import { loadPyodide, version } from 'pyodide';
import type { PyodideInterface } from 'pyodide';
import type { PyProxy } from 'pyodide/ffi';
Expand Down Expand Up @@ -60,7 +60,8 @@ async function createAPI(pyodide: PyodideInterface) {
# setup backend:
bumps.cli.install_plugin(refl1d.fitplugin)
refl1d.use('c_ext')
js_server_instance = object()
wrapped_api = {}
def expose(method, method_name):
Expand All @@ -77,11 +78,15 @@ async function createAPI(pyodide: PyodideInterface) {
async def worker_fit_progress_handler(serialized_event):
event = dill.loads(serialized_event)
if event.get("message") == "uncertainty_update":
# call back into the JavaScript server to sync the filesystem
await js_server_instance.syncFS()
await api._fit_progress_handler(event)
async def worker_fit_complete_handler(serialized_event):
event = dill.loads(serialized_event)
await api._fit_complete_handler(event)
await js_server_instance.syncFS()
wrapped_api["evt_fit_progress"] = expose(worker_fit_progress_handler, "evt_fit_progress")
wrapped_api["evt_fit_complete"] = expose(worker_fit_complete_handler, "evt_fit_complete")
Expand Down Expand Up @@ -117,6 +122,7 @@ async function createAPI(pyodide: PyodideInterface) {
async def _run(self):
dumped = dill.dumps(self.problem)
await api.emit("set_fit_thread_autosave_session_interval", self.uncertainty_update)
await api.emit("set_fit_thread_problem", dumped)
await api.emit("start_fit_thread_fit", self.fitclass.id, self.options, self.terminate_on_finish)
await api.emit("add_notification", {
Expand Down Expand Up @@ -150,6 +156,7 @@ export class Server {
pyodide: PyodideInterface;
api: PyProxy;
initialized: Promise<void>;
fit_server: Remote<FitServer>;

constructor() {
this.handlers = {};
Expand All @@ -175,19 +182,25 @@ export class Server {
this.api = api;
await this.asyncEmit("server_startup_status", {status: "api created", percent: 100});
const fit_server = await FitServerPromise;
this.fit_server = fit_server;
const abort_fit_signal = new Signal("fit_abort_event");
const fit_complete_signal = new Signal("fit_complete_event");
await fit_server.set_signal(abort_fit_signal);
await this.set_signal(abort_fit_signal);
await fit_server.set_signal(fit_complete_signal);
await this.set_signal(fit_complete_signal);
const defineEmit = await pyodide.runPythonAsync(`
def defineEmit(server):
api.emit = server.asyncEmit;
const defineJSServer = await pyodide.runPythonAsync(`
def defineJSServer(js_server):
global js_server_instance
js_server_instance = js_server
api.emit = js_server.asyncEmit;
defineEmit
defineJSServer
`);
await defineEmit(this);
await defineJSServer(this);
this.addHandler('set_fit_thread_autosave_session_interval', async (interval: number) => {
const result = await fit_server.onAsyncEmit('set_autosave_session_interval', interval);
});
this.addHandler('set_fit_thread_problem', async (problem: any) => {
const result = await fit_server.onAsyncEmit('set_problem', problem);
console.log("set_fit_thread_problem result:", result);
Expand Down

0 comments on commit 1621d50

Please sign in to comment.