In my web application, I have a wasm process (compiled from c++) that processes data. The code for this process is as follows:
std::vector<JSONObject> data
for (size_t i = 0; i < data.size(); i++)
{
process_data(data[i]);
if (i % 1000 == 0) {
bool is_cancelled = check_if_cancelled();
if (is_cancelled) {
break;
}
}
}
This code essentially simulates running/processing a query like a SQL query interface:
https://i.sstatic.net/WKPws.png
Queries may take minutes to complete and the user has the option to cancel their query at any time. The cancellation process happens in the normal javascript/web application, not within the service Worker where the wasm is running.
My concern is how to notify the wasm process when the user cancels their query so it can exit properly. Terminating the worker with worker.terminate()
is not an option because we need to keep all loaded data for that worker. It needs to remain active so another query can be executed.
How can we effectively communicate between the javascript and worker/wasm/c++ application to know when to exit gracefully?
Additionally, let's assume a standard query takes 60s to execute and processes 500MB of data using cpp/wasm in-browser.
Update: After some research and feedback, here are some potential solutions along with their feasibility:
Using two workers - one to store data and another to process it. However, transferring large amounts of data would be time-consuming and inefficient.
Utilizing Emterpreter and
emscripten_sleep_with_yield
, but this significantly impacts performance.Running a second worker while displaying only the most recent data on UI, potentially causing memory errors.
Sending API calls to a server to track query status, but it involves frequent network requests adding unnecessary overhead.
Implementing an incremental-parsing approach by rewriting parsing functions, which could be labor-intensive.
Storing status in IndexedDB, allocating memory in WASM, and processing data in C++, which seems promising but requires further exploration.
[Any other suggestions?]
For the bounty, I'm seeking answers to these questions:
- Are the six proposed solutions valid?
- Are there better alternatives worth considering?
- Can someone demonstrate a basic example of #6, assuming it works universally across browsers?