https://github.com/koding/qfunction
Make sure specified function runs one at a time
https://github.com/koding/qfunction
Last synced: 4 months ago
JSON representation
Make sure specified function runs one at a time
- Host: GitHub
- URL: https://github.com/koding/qfunction
- Owner: koding
- License: mit
- Created: 2011-10-04T06:04:10.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2016-08-01T11:09:04.000Z (over 9 years ago)
- Last Synced: 2025-08-04T14:10:26.070Z (5 months ago)
- Language: CoffeeScript
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 13
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Why Queued Function?
In some cases, (for example adding users to linux box) if you try to run your functions in parallel it will fail. And you need very lightweight queue functionality without adding any complexity to your project.
You want to have very simple queue, that will register each incoming function, and execute it one by one.
For example (in coffeescript, for js version, scroll below):
qfunction = require "qfunction"
#
# here is an example function
# that will callback after 1 second.
#
exampleFunction = (x,callback)-> setTimeout (()-> callback "Finished in 1 sec...with #{x} #{Date.now()}"),1000
#
# if you run the for-loop below,
# they will approximately all finish at the same time
#
for i in [0..10]
exampleFunction i,(r)->
console.log "regular function out:",r
#
# now you can create a queued version of this function,
# and it will finish in 10 seconds,
# running one after another
#
queuedExampleFunction = qfunction exampleFunction
for i in [0..10]
queuedExampleFunction i,(r)->
console.log "queued function out:",r
## Installation
$ npm install qfunction
## To do
- Queue can be persisted to a db (right now, pending functions will die if node process dies)
- Functions that don't have a callback can be serialized (however, i don't know why those need to be serialized)
## Example in JS
var exampleFunction, i, qfunction, queuedExampleFunction;
qfunction = require("qfunction");
exampleFunction = function(x, callback) {
return setTimeout((function() {
return callback("Finished in 1 sec...with " + x + " " + (Date.now()));
}), 1000);
};
for (i = 0; i <= 10; i++) {
exampleFunction(i, function(r) {
return console.log("regular function out:", r);
});
}
queuedExampleFunction = qfunction(exampleFunction);
for (i = 0; i <= 10; i++) {
queuedExampleFunction(i, function(r) {
return console.log("queued function out:", r);
});
}