runAsync Clarification

Just found out that lucee has the runAsync function to allow code to be run asynchronously which is awesome, however after doing a little research I’m unsure if it supports the way I’m looking to use it.

I have a database logging operation I’m looking to run asynchronously. Currently the code that does this logging is in a custom tag. The data I need to log is passed via attributes to this tag, wherein the insert query is then executed. Really simple and flexible, and it allows me to easily add the logging anywhere in our code.
My hope then, is that I’m able to keep most of the code the same, with the only different being the query is executed asynchronously. However, based off what I’ve read about the runAsync, there must always be a place where you get the results of the future object; I think someone said in another thread it’s basically the equivalent of threads.join. I wanted to see if that was indeed the case, or if there was a way to set it to run asynchronously, then just ignore it.
In this situation, once I have it ready to run asynchronously, I don’t care about it anymore. I don’t need a response back. I don’t even care if it throws an error; it’s just a log.

So, is that safe to do, or does it potentially message with lucee’s thread management?

If you need the result of the operation, then why are you running it async if you’re just going to wait on it. If you want to fire-and-forget, you can also just use CFThread.

Maybe I didn’t phrase it correctly, but I don’t need the result of the operation, and I don’t want to wait on it.
Thanks for the suggestion of cfthread; I didn’t realize it could be used that way. All the documentation I’ve seen in the past on cfthread in the past has either said or implied that you HAD to join all the threads together , so I never realized you could use it that way.

Yeah, you can always do

thread name="whatever" {
  // Logic I don't care to wait for
}

and then just never join to it. runAsync is just a (rather poorly thought out) functional approach to threading. If you just want to fire-and-forget, there’s really no reason IMO to use the run async per se unless you just really want to.

If you’re interested in the whole promise-based async stuff, ditch runAsync, and look at the much more powerful async package available in Coldbiox/WireBox/CacheBox/LogBox. It’s a direct wrapper to the java concurrent package and is not nearly as limited. It’s basically what runAsync SHOULD have been, lol. The docs are for ColdBox, but you can use it in any app so long as you just drop in the standlone version of wire/cache/logbox.

2 Likes

Awesome, much appreciated!

1 Like