Sumber dimuat naik... memuat...

Tugas batch pelbagai benang

Penulis:Satu pukulan lelaki, Tarikh: 2019-07-31 16:19:52
Tag:Alat

    var tasks = $.go([
        [exchange, 'GetDepth'],
        [exchange, 'GetTicker'],
    ])
    var retList = $.wait(tasks)
    Log(retList[0])
    Log(retList[1])
  • $.go (dalam bahasa Inggeris)

Melakukan Go dalam jumlah besar Jenis parameter Array dua dimensi

   [
        [exchange, 'GetDepth'],
        [exchange, 'GetTicker'],
    ]

Kembali nilai cmdList Array

  • $.wait ((cmdList)

Menerima hasil

  • $. gowait (()

Melakukan Go dan mendapatkan hasilnya


$.go = function(tasks) {
    return _.map(tasks, function(args) {
        return {
            result: null,
            args: args,
            defer: args[0].Go.apply(args[0], args.slice(1))
        }
    })
}

$.gowait = function(tasks, maxConcurrent, retry) {
    return $.wait($.go(tasks), maxConcurrent, retry)
}

$.wait = function(cmdList, maxConcurrent, retry) {
    retry = retry || true
    maxConcurrent = Math.min(cmdList.length, maxConcurrent || 5)
    var total = Math.ceil(cmdList.length / maxConcurrent)
    return _.reduce(_.range(total), function(beforeResultList, currentPage) {
        var lastCmdList = cmdList.slice(currentPage * maxConcurrent, currentPage * maxConcurrent + maxConcurrent)
        while (1) {
            lastCmdList = _.map(lastCmdList, function(cmd) {
                var args = cmd.args
                if (cmd.result == null) {
                    var ret = cmd.defer.wait()
                    if (ret != null) {
                        return _.extend(cmd, {
                            result: ret
                        })
                    } else if (retry === true) {
                        return _.extend(cmd, {
                            defer: args[0].Go.apply(args[0], args.slice(1)),
                            result: null
                        })
                    }
                }
                return cmd
            })
            var isFinished = _.every(lastCmdList, function(cmd) {
                return cmd.result
            })
            if (isFinished) {
                break
            }
            Sleep(RETRY_INTERVAL * 1000)
        }
        var newResultList = lastCmdList.map(function(cmd) {
            return cmd.result
        })
        if (currentPage + 1 < total) {
            Sleep(1000)
        }
        return beforeResultList.concat(newResultList)
    }, [])
}

Berkaitan

Lebih lanjut

FmzeroOh, itu sombong.

RumputBaguslah.