From c45405f608371bce4208a3deb80ba1bf91d76498 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 21 Jan 2014 05:02:45 +0400 Subject: [PATCH] worker pool now working... Signed-off-by: Alex A. Naanou --- ui/files.js | 5 +++-- ui/lib/jli.js | 61 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/ui/files.js b/ui/files.js index 2d720580..2af07e9c 100755 --- a/ui/files.js +++ b/ui/files.js @@ -843,12 +843,12 @@ function exportImagesTo(path, im_name, dir_name, size){ var z = (('10e' + (selection.length + '').length) * 1 + '').slice(2) var res = $.Deferred() - var pool_size = 100 - var pool = makeDefferedPool(pool_size) + var pool = makeDefferedPool() .depleted(function(){ showStatusQ('Export: done.') res.resolve() }) + window.Pool = pool // go through ribbons... for(var i=DATA.ribbons.length-1; i >= 0; i--){ @@ -866,6 +866,7 @@ function exportImagesTo(path, im_name, dir_name, size){ var o = selection.indexOf(gid) + '' dest = dest.replace('%i', (z + o).slice(o.length)) + //exportImageTo(gid, path, dest, size) pool.enqueue(null, exportImageTo, [gid, path, dest, size]) } diff --git a/ui/lib/jli.js b/ui/lib/jli.js index e6bce280..dcc0fa37 100755 --- a/ui/lib/jli.js +++ b/ui/lib/jli.js @@ -9,6 +9,8 @@ //var DEBUG = DEBUG != null ? DEBUG : true +var POOL_SIZE = 64 + /*********************************************************************/ @@ -727,15 +729,24 @@ function makeDeferredsQ(first){ // XXX should this be an object or a factory??? function makeDefferedPool(size){ - var pool = { + size = size == null ? POOL_SIZE : size + size = size < 0 ? 1 + : size > 512 ? 512 + : size + + var Pool = { pool: [], size: size, queue: [], } - pool._deplete_handlers = [] + var len = function(){ + return Pool.pool.filter(function(){ return true }).length + } - pool._run = function(obj, func, args){ + Pool._deplete_handlers = [] + + Pool._run = function(obj, func, args){ var that = this var pool = this.pool var pool_size = this.size @@ -749,9 +760,9 @@ function makeDefferedPool(size){ var i = pool.indexOf(worker) // shrink the pool if it's overfilled... - if(pool.length > pool_size){ + if(len(pool) > pool_size){ // remove self... - pool.splice(i, 1) + delete pool[i] return } @@ -761,15 +772,16 @@ function makeDefferedPool(size){ // run the next worker if it exists... if(next != null){ // replace self with next worker... - pool[i] = run.apply(that, next) + run.apply(that, next) + delete pool[i] // nothing in queue... } else { // remove self... - pool.splice(i, 1) + delete pool[i] - // empty queue and empty pool mean we are done... - if(pool.length == 0){ + // empty queue AND empty pool mean we are done... + if(len(pool) == 0){ that._deplete() } } @@ -777,49 +789,54 @@ function makeDefferedPool(size){ // keep the pool full... that._fill() }) - .fial(function(){ + .fail(function(){ // XXX + //queue.splice(0, queue.length) }) + this.pool.push(worker) + return worker } - pool._fill = function(){ + Pool._fill = function(){ var that = this - var pool = this.pool var pool_size = this.size var run = this._run - var res = [] + var l = len(this.pool) - if(pool.length < pool_size && this.queue.length > 0){ - res = this.queue.splice(0, pool_size - pool.length) + if(l < pool_size && this.queue.length > 0){ + this.queue.splice(0, pool_size - l) .forEach(function(e){ - pool.push = run.apply(that, e) + run.apply(that, e) }) } - return res + return this } - pool._deplete(){ + Pool._deplete = function(){ var that = this - this._done_handlers.forEach(function(func){ + this._deplete_handlers.forEach(function(func){ func(that) }) + return this } // public methods... - pool.enqueue = function(obj, func, args){ + Pool.enqueue = function(obj, func, args){ // add worker to queue... this.queue.push([obj, func, args]) // start work if we have not already... this._fill() + return this } // This is called after the pool is populated and depleted... - pool.depleted = function(func){ + Pool.depleted = function(func){ this._deplete_handlers.push(func) + return this } - return pool + return Pool }