【问题标题】:trying to run commands in series尝试连续运行命令
【发布时间】:2018-01-07 18:37:33
【问题描述】:

我正在尝试在 Node 中连续运行一些 shell 命令同时将一些信息发送回客户端。

为此,我结合使用 Socket.IOAsync.JS

我来自 Python,每个命令一个接一个地完美运行。据我了解,Node 可以同时运行所有东西,这很棒,但有时我们需要一个接一个地运行一些东西。

这是我的服务器端代码:

io.on('connection', function(client) {
  client.on('convertstart', function(data) {
    async.series([
        function (next) {
          console.log('# STARTING -> room ' + room_);
          client.join(data.room);
          next(null, '');
        },
        function (next) {
          io.to(data.room).emit('step0');
          next(null, '');
        },
        function (next) {
          var some_commands = require('child_process').execSync('some bash commands');
          next(null, '');
        },
        function (next) {
          io.to(data.room).emit('step1');
          next(null, '');
        },
        function (next) {
          var some_commands = require('child_process').execSync('some other bash commands');
          next(null, '');
        }
    ],
    function (err, result) {
        console.log(result);
    });
  });
});

这是我的客户端代码:

socket.on('step0', function(data){
  for(var i = 0; i < 10; i++) {
    (function(i){
      setTimeout(function(){
        $(".uk-progress").css('width', i + '%');
        $(".uk-progress").text(i + '%');
    }, 300 * i)
   })(i);
  }
});

socket.on('step1', function(data){
  for(var i = 10; i < 30; i++) {
    (function(i){
      setTimeout(function(){
        $(".uk-progress").css('width', i + '%');
        $(".uk-progress").text(i + '%');
    }, 300 * i)
   })(i);
  }
});

想法是将进度条从 0% 移动到 10%(使用 Socket.IO),然后做一些命令行操作,然后将进度条从 10% 移动到 30%(再次使用 Socket.IO) ,然后再做一些命令行操作等等。

目前,此代码只运行整个内容,而不会在每个操作之间停止。我想注意每个命令行命令需要 5 到 10 秒,所以进度条的进度不可能是线性的。

【问题讨论】:

    标签: javascript node.js sockets websocket async.js


    【解决方案1】:

    乍一看...问题似乎与next()与它需要等待的命令同步使用的事实有关...但我相信它实际上应该作为成功回调传递您需要运行的命令的函数。

    所以:

    function (next) {
       console.log('# STARTING -> room ' + room_);
       client.join(data.room);
       next(null, '');
    },
    

    实际上是:

    function (next) {
       console.log('# STARTING -> room ' + room_);
       // Use next as the callback once client joins.
       client.join(data.room, next);  // use next.bind(null, '') if needed.
    },
    

    每一步都以此类推。

    我相信,发出回调是第二或第三个选项,所以:.emit('step1', next).emit('step1',null,next) 应该这样做。

    https://socket.io/docs/#sending-and-getting-data-(acknowledgements)

    您将不得不切换到异步子进程执行以使用回调,这正是您想要的,所以:.exec('some bash commands', next)

    https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

    【讨论】:

    • 感谢您的回答@ReyHaynes,我只是尝试了所有可能的组合,但它不起作用:我收到错误Error: Callbacks are not supported when broadcasting...
    • 我去过的最远的地方是当我为第一次操作执行client.join(room_, callback.bind(null, ''));,然后它运行第二个操作 (io.to(room_).emit('step0');) 但它停在那里。它不会运行剩下的东西。
    • 我已尝试按照您的建议使用其他一些内容运行第二个命令,例如 io.to(room_).emit('step0', null,next);io.to(room_).emit('step0',next); 甚至 io.to(room_).emit('step0', null,next.bind(null, '')); 但没有按预期工作。它要么并行运行整个事情,要么停止并且不运行其余部分。
    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2021-01-10
    • 2014-10-23
    • 2018-11-05
    • 1970-01-01
    • 2020-07-25
    相关资源
    最近更新 更多