最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js - Pausing script execution in another thread - Stack Overflow

programmeradmin5浏览0评论

.:Disclaimer:.

Please note that I am trying to achieve something with node.js which might go against its design principles. However as node.js goes against the norm of "javascript is for client side", I am also trying something different here. Please bear with me.

.:Background:.

I have a requirement where Java Scripts need to be narrative (read from beginning to end) for simplistic scripts for simplistic users. I will also offer an Async scripting ability for more advanced users.

I understand the event driven methodology of node.js, and have a solution working based on Async callbacks. Now I am trying to simplify this for our more basic scripting requirements.

.:The Question:.

Is there a way to run a script (using its own sandbox) where execution can be paused while a result is being delivered.

E.g.

var util = require('util'),
vm = require('vm'),
user = require('user');

vm.runInThisContext('user.WaitForProceed(); console.log('Waiting over');');

console.log('Finished!');

User is my own module that will do the waiting. Basically I want it to sit there and block on that line in the vm object until it has received a result back. After which it will continue onto the console.log line.

The output of this example unimportant as it is also achievable through callbacks. The narrative nature of the example script is more important for this solution.

  • J

.:Disclaimer:.

Please note that I am trying to achieve something with node.js which might go against its design principles. However as node.js goes against the norm of "javascript is for client side", I am also trying something different here. Please bear with me.

.:Background:.

I have a requirement where Java Scripts need to be narrative (read from beginning to end) for simplistic scripts for simplistic users. I will also offer an Async scripting ability for more advanced users.

I understand the event driven methodology of node.js, and have a solution working based on Async callbacks. Now I am trying to simplify this for our more basic scripting requirements.

.:The Question:.

Is there a way to run a script (using its own sandbox) where execution can be paused while a result is being delivered.

E.g.

var util = require('util'),
vm = require('vm'),
user = require('user');

vm.runInThisContext('user.WaitForProceed(); console.log('Waiting over');');

console.log('Finished!');

User is my own module that will do the waiting. Basically I want it to sit there and block on that line in the vm object until it has received a result back. After which it will continue onto the console.log line.

The output of this example unimportant as it is also achievable through callbacks. The narrative nature of the example script is more important for this solution.

  • J
Share Improve this question asked Mar 5, 2012 at 5:51 NevarNevar 611 silver badge5 bronze badges 4
  • So who proclaimed this so called NORM(!?) "javascript is for client side" - or is it that you (and others) only ever saw it there and declared the only thing YOU/THEY knew must therefore be a "norm"? Anyway, no there is no way to pause. The JS machine continues to run the event loop and only stops if there is nothing to be run. So the only way to stop is to have no more code and no more events (with an event listener attached). The only thing you can do is wait for that"proceed" event. – Mörre Commented Mar 5, 2012 at 7:39
  • ...so you have to have an event to listen to, fired by your "user" object. But you said you already have such a solution, so the important thing for you to take away from this is how the JS engine itself actually works, then you'll get a better feeling for this stuff :) You should tell us more about what user.WaitForProcceed() does ;) – Mörre Commented Mar 5, 2012 at 7:52
  • "...so the important thing for you to take away from this is how the JS engine itself actually works..." Norm is how others tell me things "should work". I understand the limitations and functionality of node, however as with all developers I deal with people that don't. They however want to use this system, in their way. I simply explore what is possible. – Nevar Commented Mar 6, 2012 at 22:05
  • Think of "user.WaitForProceed();" as a blocker, it stops the code from running any future until asked to wake up again. Like @jp-richardson example this would block the main message loop. It is this that I am looking to avoid. – Nevar Commented Mar 6, 2012 at 22:08
Add a ment  | 

3 Answers 3

Reset to default 8

There is no way to pause the execution in Node. At least they tell us so :)

There are some libraries which support an advanced flow control, like Invoke, maybe this could help, but I understands, that's not what you asked for :)

Also you could implement a busy-loop using nextTick()

Or you could implement a blocking call in C(++) and provide it as a library. I never did this.

One last way is to readFileSync() to a namedpipe which closes on a certian event.

As you already mentioned, it's against the language principes, therefor these solutions are all hacky.

If you really want to sleep the execution of the Node process, you can. However, as you state, it seems that you're fully aware of the implications.

Here is an NPM module to do this: https://github./ErikDubbelboer/node-sleep

You can use it like so:

var sleep = require('sleep');
sleep.sleep(1); //sleep for 1 sec
sleep.usleep(2000000); //sleep for 2 sec

I partially write this for future visitors that arrive by Google search: You should not use the aforementioned technique. If you decide that you must, be aware that this will block your Node process and it won't be able to do any additional work until the sleep period is over. Additionally, you will violate every expectation of any user who is aware that it's Node.js programs, as Node.js programs are suppose to be non-blocking.

Good luck.

If you realy want to pause execution while waiting for result you may try to work with node-sync. It build on node-fibers. Your application needs to be executed with node-fibers script instead of node. node-sync adds sync method to Function.prototype that allows to run it syncroniously.

Also you need to wrap your call in the fiber (thread) so as not to block the event-loop.

var Sync = require('sync');

var someAsyncFunction = function(a, b, callback) {
    setTimeout(function() {
        var result = a + b;
        callback(undefined, result);
    }, 1000);
};

// Run in a fiber
Sync(function(){
    // This code runs in separate fiber and does not block the event loop

    // First argument is 'this' context
    var result = someAsyncFunction.sync(null, 2, 3);
    // Waiting one second
    console.log(result); // Will output 5
});

// Event loop here

Please be careful with it. You need to understand that is not the node way.

发布评论

评论列表(0)

  1. 暂无评论