3

In Grails service, I would like to render a page while another method is running asynchronously. I tried different things from the documentation async and gpars library. None of the listed things worked as desired.

GParsPool.withPool(2) {
    this.&longFunction.callAsync(command)
    rendering()
}

later tried

    GParsPool.withPool(2) {
        def a ={longFunction(command)}.async()
        a()
        rendering()
    }
Etibar - a tea bar
  • 1,912
  • 3
  • 17
  • 30
  • Could you share with us more details about your problem? What version of Grails you use? From this short part of code I can only suggest two things: try to investigate if both functions are actually run by two different threads (e.g. https://gist.github.com/wololock/3228fccfe5058c93d69f29cdb7382059), alternatively you could try using `Promise` to run async task (https://docs.grails.org/latest/guide/async.html) – Szymon Stepniak Jul 12 '17 at 13:39
  • @Szymon Stepniak, thank you for a comment. I am using grails 3.1. With promise the codes are running, in front end still it doesn't render anything until promise finishes running. I tried to code you shared. Still, the front end which is angular project waits long function to finish. the methods are running async, however, frontend waits async process to finish. – Etibar - a tea bar Jul 12 '17 at 14:09
  • Hmm, rendering Angular frontend in service sounds weird to me. What does this longFunction do? If it provides data that frontend has to display you may need to rethink the flow in your application. If Grails app plays role of REST backend you may try something like this: Angular app calls request to backend that runs this long function async and returns something like "check status" link. Your Angular app then may use this link that was returned immediately to call it e.g. every 1 sec to check if computation of long function is done. This is only a big picture I draw without knowing details – Szymon Stepniak Jul 12 '17 at 14:35
  • @Szymon Stepniak, longfunction doesn't return anything. before long function all the needed information are validated and after that I would like to render a page that everything is fine and at the same time want to start async method. However, I don't really understand what might cause angular frontend to wait long function. I used the method you shared in Github, the frontend still waited for it, even though it was stateless pure method. Grails app plays rest backend. – Etibar - a tea bar Jul 12 '17 at 15:08

0 Answers0