Giter Site home page Giter Site logo

co-parallel's Introduction

co-parallel

Parallel execution with concurrency support that maintains result ordering.

Installation

$ npm install co-parallel

Example

var parallel = require('co-parallel');
var request = require('co-request');
var co = require('co');

var urls = [
  'http://google.com',
  'http://yahoo.com',
  'http://ign.com',
  'http://cloudup.com',
  'http://myspace.com',
  'http://facebook.com',
  'http://segment.io'
];

function *status(url) {
  console.log('GET %s', url);
  return (yield request(url)).statusCode;
}

co(function *(){
  var reqs = urls.map(status);
  var res = yield parallel(reqs, 2);
  console.log(res);
})();

API

parallel(thunks, [concurrency])

Execute thunks in parallel, with the given concurrency defaulting to 5.

License

MIT

co-parallel's People

Contributors

dead-horse avatar tj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

co-parallel's Issues

Can't run example: SyntaxError: Unexpected token *

Not sure what I did wrong, but I can't run the example...

mdesales@Marcello-Work ~/dev/github/co-parallel (master) $ npm install
npm http GET https://registry.npmjs.org/co-thread/0.0.1
npm http GET https://registry.npmjs.org/co
npm http GET https://registry.npmjs.org/co-request
npm http GET https://registry.npmjs.org/mocha
npm http GET https://registry.npmjs.org/should
npm http 304 https://registry.npmjs.org/co-thread/0.0.1
npm http 304 https://registry.npmjs.org/co
npm http 304 https://registry.npmjs.org/co-request
npm http GET https://registry.npmjs.org/co-request/-/co-request-0.1.2.tgz
npm http 200 https://registry.npmjs.org/should
npm http GET https://registry.npmjs.org/should/-/should-3.3.0.tgz
npm http 200 https://registry.npmjs.org/mocha
npm http 200 https://registry.npmjs.org/co-request/-/co-request-0.1.2.tgz
npm http 200 https://registry.npmjs.org/should/-/should-3.3.0.tgz
npm http GET https://registry.npmjs.org/request
npm http GET https://registry.npmjs.org/commander/2.0.0
npm http GET https://registry.npmjs.org/growl
npm http GET https://registry.npmjs.org/jade/0.26.3
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/diff/1.0.7
npm http GET https://registry.npmjs.org/glob/3.2.3
npm http GET https://registry.npmjs.org/mkdirp/0.3.5
npm http 304 https://registry.npmjs.org/request
npm http GET https://registry.npmjs.org/json-stringify-safe
npm http GET https://registry.npmjs.org/forever-agent
npm http GET https://registry.npmjs.org/node-uuid
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/commander/2.0.0
npm http GET https://registry.npmjs.org/form-data
npm http GET https://registry.npmjs.org/tough-cookie
npm http GET https://registry.npmjs.org/tunnel-agent
npm http GET https://registry.npmjs.org/http-signature
npm http GET https://registry.npmjs.org/oauth-sign
npm http GET https://registry.npmjs.org/hawk
npm http GET https://registry.npmjs.org/aws-sign2
npm http 304 https://registry.npmjs.org/jade/0.26.3
npm http 304 https://registry.npmjs.org/diff/1.0.7
npm http 304 https://registry.npmjs.org/glob/3.2.3
npm http 304 https://registry.npmjs.org/json-stringify-safe
npm http 200 https://registry.npmjs.org/debug
npm http 200 https://registry.npmjs.org/mkdirp/0.3.5
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http 304 https://registry.npmjs.org/forever-agent
npm http 304 https://registry.npmjs.org/node-uuid
npm http 304 https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/growl
npm http 304 https://registry.npmjs.org/form-data
npm http 304 https://registry.npmjs.org/tunnel-agent
npm http 304 https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/http-signature
npm http 304 https://registry.npmjs.org/oauth-sign
npm http 304 https://registry.npmjs.org/hawk
npm http 304 https://registry.npmjs.org/aws-sign2
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http 304 https://registry.npmjs.org/tough-cookie
npm http GET https://registry.npmjs.org/combined-stream
npm http GET https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/assert-plus/0.1.2
npm http GET https://registry.npmjs.org/asn1/0.1.11
npm http GET https://registry.npmjs.org/ctype/0.5.2
npm http GET https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/graceful-fs
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/punycode
npm http GET https://registry.npmjs.org/hoek
npm http GET https://registry.npmjs.org/boom
npm http GET https://registry.npmjs.org/cryptiles
npm http GET https://registry.npmjs.org/sntp
npm http GET https://registry.npmjs.org/mkdirp/0.3.0
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http 304 https://registry.npmjs.org/combined-stream
npm http 304 https://registry.npmjs.org/async
npm http 304 https://registry.npmjs.org/asn1/0.1.11
npm http 304 https://registry.npmjs.org/assert-plus/0.1.2
npm http 304 https://registry.npmjs.org/ctype/0.5.2
npm http 304 https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/delayed-stream/0.0.5
npm http 304 https://registry.npmjs.org/graceful-fs
npm http 304 https://registry.npmjs.org/inherits
npm http 304 https://registry.npmjs.org/punycode
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
npm http 200 https://registry.npmjs.org/mkdirp/0.3.0
npm http 304 https://registry.npmjs.org/boom
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz
npm http 304 https://registry.npmjs.org/cryptiles
npm http 304 https://registry.npmjs.org/commander/0.6.1
npm http 304 https://registry.npmjs.org/delayed-stream/0.0.5
npm http 304 https://registry.npmjs.org/sntp
npm http 304 https://registry.npmjs.org/lru-cache
npm http 304 https://registry.npmjs.org/sigmund
npm http 304 https://registry.npmjs.org/hoek
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz
[email protected] node_modules/co-thread

[email protected] node_modules/co

[email protected] node_modules/should

[email protected] node_modules/co-request
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])

[email protected] node_modules/mocha
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected])
mdesales@Marcello-Work ~/dev/github/co-parallel (master) $ node example.js

/Users/mdesales/dev/github/co-parallel/example.js:22
function *status(url) {
^
SyntaxError: Unexpected token *
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3

Add support for non-array objects

It would be great if this followed the co API and allowed parallelism of objects other than arrays. I could tee up a PR if you think this is reasonable.

Can't run example: SyntaxError: Unexpected token *

Not sure what I did wrong, but I can't run the example...

mdesales@Marcello-Work ~/dev/github/co-parallel (master) $ npm install
npm http GET https://registry.npmjs.org/co-thread/0.0.1
npm http GET https://registry.npmjs.org/co
npm http GET https://registry.npmjs.org/co-request
npm http GET https://registry.npmjs.org/mocha
npm http GET https://registry.npmjs.org/should
npm http 304 https://registry.npmjs.org/co-thread/0.0.1
npm http 304 https://registry.npmjs.org/co
npm http 304 https://registry.npmjs.org/co-request
npm http GET https://registry.npmjs.org/co-request/-/co-request-0.1.2.tgz
npm http 200 https://registry.npmjs.org/should
npm http GET https://registry.npmjs.org/should/-/should-3.3.0.tgz
npm http 200 https://registry.npmjs.org/mocha
npm http 200 https://registry.npmjs.org/co-request/-/co-request-0.1.2.tgz
npm http 200 https://registry.npmjs.org/should/-/should-3.3.0.tgz
npm http GET https://registry.npmjs.org/request
npm http GET https://registry.npmjs.org/commander/2.0.0
npm http GET https://registry.npmjs.org/growl
npm http GET https://registry.npmjs.org/jade/0.26.3
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/diff/1.0.7
npm http GET https://registry.npmjs.org/glob/3.2.3
npm http GET https://registry.npmjs.org/mkdirp/0.3.5
npm http 304 https://registry.npmjs.org/request
npm http GET https://registry.npmjs.org/json-stringify-safe
npm http GET https://registry.npmjs.org/forever-agent
npm http GET https://registry.npmjs.org/node-uuid
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/commander/2.0.0
npm http GET https://registry.npmjs.org/form-data
npm http GET https://registry.npmjs.org/tough-cookie
npm http GET https://registry.npmjs.org/tunnel-agent
npm http GET https://registry.npmjs.org/http-signature
npm http GET https://registry.npmjs.org/oauth-sign
npm http GET https://registry.npmjs.org/hawk
npm http GET https://registry.npmjs.org/aws-sign2
npm http 304 https://registry.npmjs.org/jade/0.26.3
npm http 304 https://registry.npmjs.org/diff/1.0.7
npm http 304 https://registry.npmjs.org/glob/3.2.3
npm http 304 https://registry.npmjs.org/json-stringify-safe
npm http 200 https://registry.npmjs.org/debug
npm http 200 https://registry.npmjs.org/mkdirp/0.3.5
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http 304 https://registry.npmjs.org/forever-agent
npm http 304 https://registry.npmjs.org/node-uuid
npm http 304 https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/growl
npm http 304 https://registry.npmjs.org/form-data
npm http 304 https://registry.npmjs.org/tunnel-agent
npm http 304 https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/http-signature
npm http 304 https://registry.npmjs.org/oauth-sign
npm http 304 https://registry.npmjs.org/hawk
npm http 304 https://registry.npmjs.org/aws-sign2
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http 304 https://registry.npmjs.org/tough-cookie
npm http GET https://registry.npmjs.org/combined-stream
npm http GET https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/assert-plus/0.1.2
npm http GET https://registry.npmjs.org/asn1/0.1.11
npm http GET https://registry.npmjs.org/ctype/0.5.2
npm http GET https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/graceful-fs
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/punycode
npm http GET https://registry.npmjs.org/hoek
npm http GET https://registry.npmjs.org/boom
npm http GET https://registry.npmjs.org/cryptiles
npm http GET https://registry.npmjs.org/sntp
npm http GET https://registry.npmjs.org/mkdirp/0.3.0
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http 304 https://registry.npmjs.org/combined-stream
npm http 304 https://registry.npmjs.org/async
npm http 304 https://registry.npmjs.org/asn1/0.1.11
npm http 304 https://registry.npmjs.org/assert-plus/0.1.2
npm http 304 https://registry.npmjs.org/ctype/0.5.2
npm http 304 https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/delayed-stream/0.0.5
npm http 304 https://registry.npmjs.org/graceful-fs
npm http 304 https://registry.npmjs.org/inherits
npm http 304 https://registry.npmjs.org/punycode
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
npm http 200 https://registry.npmjs.org/mkdirp/0.3.0
npm http 304 https://registry.npmjs.org/boom
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz
npm http 304 https://registry.npmjs.org/cryptiles
npm http 304 https://registry.npmjs.org/commander/0.6.1
npm http 304 https://registry.npmjs.org/delayed-stream/0.0.5
npm http 304 https://registry.npmjs.org/sntp
npm http 304 https://registry.npmjs.org/lru-cache
npm http 304 https://registry.npmjs.org/sigmund
npm http 304 https://registry.npmjs.org/hoek
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz
[email protected] node_modules/co-thread

[email protected] node_modules/co

[email protected] node_modules/should

[email protected] node_modules/co-request
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])

[email protected] node_modules/mocha
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected])
mdesales@Marcello-Work ~/dev/github/co-parallel (master) $ node example.js

/Users/mdesales/dev/github/co-parallel/example.js:22
function *status(url) {
^
SyntaxError: Unexpected token *
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3

Document why this is better than yield array

Why is co-parallel better than simply yielding an array within co? I can't find any documentation about that, and my tests don't show any particular advantage:

// tasks is an array of 200 async tasks simulated by setTimeout, cf #3
var start = Date.now();
yield parallel(tasks, 10);
console.log('total used %dms', Date.now() - start);

This takes 170ms on average, while:

// tasks is an array of 200 async tasks simulated by setTimeout, cf #3
var start = Date.now();
yield tasks;
console.log('total used %dms', Date.now() - start);

This takes 117ms on average

Understanding error handling

If I run the code as shown on the README at https://github.com/tj/co-parallel#example (I've removed the .() at the end of the co function since I'm using co version 4, on node with --harmony flag), the output is as I'd expect, with the seven 200 messages (status codes) appearing for each of the requests.

GET http://google.com
GET http://yahoo.com
GET http://ign.com
GET http://cloudup.com
GET http://myspace.com
GET http://facebook.com
GET http://segment.io
[ 200, 200, 200, 200, 200, 200, 200 ]

I'm trying to understand how error handling works. If I change the demo in the README to throw Errors (first 3 lines under status function) and add an error handler to the main co function (last 3 lines below), I get some behaviour I don't understand as I'm new to generators and asychronous.

function *status(url) {
  if (url.contains('yahoo')) {
      throw Error('my error');
  }
  console.log('GET %s', url);
  return (yield request(url)).statusCode;
}

...

co(function *(){
  var reqs = urls.map(status);
  var res = yield parallel(reqs, 2);
  console.log(res);
}).catch(function(e) {
  console.log(e);
});

If I run it with the above code, the output becomes the following. Instead of yahoo.com appearing I get the error message in its place, which makes sense. But I don't get the final array of any 200 messages appearing.

GET http://google.com
[Error: my error]
GET http://ign.com
GET http://cloudup.com
GET http://myspace.com
GET http://facebook.com
GET http://segment.io

And if I add another url to the error condition, changing it to if (url.contains('yahoo') || url.contains('cloudup')) { the output becomes this:

GET http://google.com
[Error: my error]
GET http://ign.com

Here the yahoo.com line is still replaced with the error message, and ign.com appears after it, but everything after that fails to appear. And the final array of any 200 messages still doesn't appear. Why does it work like this?

Can anyone share how error handling works in co with generators, etc? Am I seeing expected behaviour with both sets of modifications I've made above? I've read some articles about it but I'm unclear about it.

concurrency a little confuse

I want to use co-parallel in my project to limit the http request to a third-party server.
just like example,limie concurrency 2:
var res = yield parallel(reqs, 2);
Is it finished two request and then start another two request? till all request finshed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.