Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active June 6, 2016 12:54
Show Gist options
  • Save roryl/557d57289456fb39e8e35765e8a0d65b to your computer and use it in GitHub Desktop.
Save roryl/557d57289456fb39e8e35765e8a0d65b to your computer and use it in GitHub Desktop.
Lucee Concurrency Examples
<cfscript>
doWork = function(){
moreWork = function(){
sleep(50);
return 5;
}
moreTasks = [
moreWork,
moreWork,
moreWork
];
var moreResult = moreTasks.map(closure=function(func){
return func();
},parallel=true);
sleep(100);
return moreResult;
}
tasks = [
doWork,
doWork,
doWork,
doWork,
doWork,
doWork,
doWork,
doWork,
doWork,
doWork
];
timer type="outline" {
result = tasks.map(closure = function(func){
return func();
});
}
timer type="outline" {
result = tasks.map(closure = function(func){
return func();
}, parallel=true);
}
timer type="outline" {
result = tasks.map(closure = function(func){
return func();
}, parallel=true, maxThreads=4);
}
writeDump(result);
writeDump(tasks);
</cfscript>
/**
* Implements a future to allow for asyncrhonous code execution
*
*/
component accessors="true" {
property name="name" hint="The unique name for the background thread";
property name="error" hint="Any exception received while executing the task";
public function init(required function task, function success, function error, function finally){
variables.name = hash(serialize(func));
thread name="#variables.name#" action="run" func="#func#" {
try {
variables.result = func();
} catch (any e){
variables.error = e;
}
}
}
public function get(){
thread action="join" name="#variables.name#";
if(structKeyExists(variables,"error")){
throw(variables.error);
}
return variables.result;
}
public function hasError(){
thread action="join" name="#variables.name#";
writeDump(variables);
abort;
return structKeyExists(variables,"error");
}
}
<cfscript>
someValue = "test closure";
task = new future(function(){
sleep(2000);
return someValue;
});
//Do some other processing
sleep(3000);
writeDump(task.getName());
writeDump(task.get());
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment