Last active
June 6, 2016 12:54
-
-
Save roryl/557d57289456fb39e8e35765e8a0d65b to your computer and use it in GitHub Desktop.
Lucee Concurrency Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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