Skip to content

Commit

Permalink
Merge pull request #38 from daniel-ji/main
Browse files Browse the repository at this point in the history
Add stdout/stderr streaming config option: printStream
  • Loading branch information
robertaboukhalil committed Jul 18, 2023
2 parents 944882c + f0c090e commit 2f228af
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/examples/stream-std.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h4>Streamed Output of <code>tn93</code>:</h4>
<pre id="output">Loading...</pre>

<h4>Pairwise distances:</h4>
<pre id="result">Loading...</pre>

<script type="module" src="/src/examples/stream-std.js"></script>
17 changes: 17 additions & 0 deletions src/examples/stream-std.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Aioli from "../../dist/aioli.mjs";

const CLI = await new Aioli(["tn93/1.0.11"], {
debug: true,
printStream: true,
callback: (d) =>{
document.getElementById("output").innerHTML += "\n<span style='color: red'>timestamp: " + performance.now() + "</span>\n";
document.getElementById("output").innerHTML += d.stdout;
}
});

// Calculate pairwise distances of sequences in test.fas
const output = await CLI.exec("tn93 -t 0.05 -o test.dst /shared/tn93/test.fas");
const result = await CLI.cat("test.dst");

// Output results
document.getElementById("result").innerHTML = result;
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const CONFIG_DEFAULTS = {
dirData: "/data",
// Interleave stdout/stderr. If set to false, `.exec()` returns an object { "stdout": <text>, "stderr": <text> }
printInterleaved: true,
// Stream stdout/stderr continuously to the main thread, instead of waiting for the WebWorker to finish running a command.
printStream: false,

// Callback function to run whenever we receive a message from the WebWorker with payload { type: "biowasm", value: ... }.
// See <https://github.com/biowasm/biowasm/tree/main/tools/bhtsne> for an example of how this can be used to send regular updates
Expand Down
24 changes: 22 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,28 @@ const aioli = {
}
},
// Setup print functions to store stdout/stderr output
print: text => tool.stdout += `${text}\n`,
printErr: aioli.config.printInterleaved ? text => tool.stdout += `${text}\n` : text => tool.stderr += `${text}\n`
print: text => {
tool.stdout += `${text}\n`;
if(aioli.config.printStream)
postMessage({
type: "biowasm",
value: {
stdout: text,
},
});
},
printErr: text => {
const destination = aioli.config.printInterleaved ? "stdout" : "stderr";
tool[destination] += `${text}\n`;
if(aioli.config.printStream)
postMessage({
type: "biowasm",
value: {
[destination]: text,
},
});
}
}
});

// -----------------------------------------------------------------
Expand Down

0 comments on commit 2f228af

Please sign in to comment.