-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async function blocks other threads from executing? #196
Comments
I think that get a proper answer to your question you will need to be more specific about what you mean by "threads". JS-Interpreter is single-threaded (like any other normal JavaScript runtime is if you're not using Web Workers). How are you creating these 'threads'—by calling Functions created by There is a way to have multiple threads running 'in parallel' by creating multiple Alternatively, have you considered just having the interpreted code call the (interpreted) As an aside:
The |
Hi @cpcallen , really appreciate the quick, detailed response! My apologies, when I said 'thread', I was referring to threads as in multiple 'parallel' Interpreter instances as shown in the JS-Interpreter Thread Demo as you mentioned. This is the code I am testing with, which I based off the above demo. I modified it so that the code is run when the run button is pressed instead of having to click parse and step. continously.
The goal is to be able to run two async programs in each thread independently of each other. |
Ah hah: this is actually a documentation bug! The error is here, in code that you have copied verbatim from thread.html (paraphrased for clarity): // Create an interpreter whose global scope will be the cross-thread global.
var myInterpreter = new Interpreter('', initAlert);
var threads = [];
function parseButton(n) {
var code = document.getElementById('code' + n).value;
var intrp = new Interpreter('');
// Replace this thread's global scope with the cross-thread global.
intrp.stateStack[0].scope = myInterpreter.globalScope;
intrp.appendCode(code);
threads[n] = intrp.stateStack;
}
function stepButton(n) {
myInterpreter.stateStack = threads[n];
var ok = myInterpreter.step();
} The problem is that @NeilFraser: not sure what the preferred fix is here. Options:
The latter seems considerably cleaner overall; the resulting code would look something like: // Create an interpreter whose global scope will be the cross-thread global.
var myInterpreter = new Interpreter('', initAlert);
var threads = [];
function parseButton(n) {
var code = document.getElementById('code' + n).value;
var intrp = new Interpreter('');
// Replace this thread's global scope with the cross-thread global.
intrp.stateStack[0].scope = myInterpreter.globalScope;
intrp.appendCode(code);
threads[n] = intrp;
}
function stepButton(n) {
var ok = threads[n].step();
} |
Apologies, I accidentally closed the issue. @cpcallen ,you're a lifesaver, there is not way I would have figured that out by myself! I implemented your change and it is working as expected now. Thanks again! |
Calling a function like this in two separate threads will result in the second thread being blocked from running until first thread callback is hit.
If I call
await(2000)
in both threads and run them simultaneously, I expect to see 'start' printed simultaneously. Instead the first 'start' will be printed followed by 'end' and then the second 'start' will be printed.Should it be possible in theory to achieve this? If so, any suggestions are appreciated! Perhaps I am approaching this from the wrong angle or there is a workaround I have not considered.
Thanks!
The text was updated successfully, but these errors were encountered: