Grails 2.3.x Promise issues

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Grails 2.3.x Promise issues

Toby Hobson
I have a requirement to retrieve a series of images by making http get requests. I'm currently using a pattern along these lines:

AtomicInteger count = new AtomicInteger(0);
for (String url : urls) {
  // PhotoImporter implements Runnable and increments the count variable
  PhotoImporter importer = new PhotoImporter(count, url);
  executorService.submit(importer);
}
 
while(count.get() < urls.length) {
  // wait with timeouts etc
}

These works fine but I thought I would try the new Promise/Task patterns in 2.3.x however I'm running into some issues with this code:

def tasks = [];
for (String url: urls) {
  def p = task {
    println url;
    // retrieve the url
  }
  tasks.add(p);
}

waitAll(tasks);

The problem is that the code is not threadsafe, it appears as if the task closure uses some form of late binding and as such if I have 5 unique urls the closure actually makes 5 requests to the last url in the urls list.

What is the correct pattern to handle such a situation?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Grails 2.3.x Promise issues (resolved I think)

Toby Hobson
I seem to have resolved the issue by replacing

for(String url : urls) {
  ...
}

with

urls.each { url ->
  ...
}

I must admit I don't fully understand why this is working, I guess I need to improve my knowledge of Groovy. If anyone can shed any light on why this works I'd be grateful.

Reply | Threaded
Open this post in threaded view
|

Re: Grails 2.3.x Promise issues (resolved I think)

Lari Hotari -
Rob Fletcher has a nice blog article about " Groovy gotcha: for loops and closure scope":
http://blog.freeside.co/post/46587122020/groovy-gotcha-for-loops-and-closure-scope

Lari

10.12.2013 18:06, Toby Hobson wrote:
I seem to have resolved the issue by replacing

for(String url : urls) {
  ...
} 

with 

urls.each { url ->
  ...
}

I must admit I don't fully understand why this is working, I guess I need to
improve my knowledge of Groovy. If anyone can shed any light on why this
works I'd be grateful. 





--
View this message in context: http://grails.1312388.n4.nabble.com/Grails-2-3-x-Promise-issues-tp4652332p4652336.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Grails 2.3.x Promise issues (resolved I think)

Toby Hobson
Thanks Lari, I'll take a look

Toby

On Tuesday, 10 December 2013, Lari Hotari [via Grails] wrote:
Rob Fletcher has a nice blog article about " Groovy gotcha: for loops and closure scope":
http://blog.freeside.co/post/46587122020/groovy-gotcha-for-loops-and-closure-scope

Lari

10.12.2013 18:06, Toby Hobson wrote:
I seem to have resolved the issue by replacing

for(String url : urls) {
  ...
} 

with 

urls.each { url ->
  ...
}

I must admit I don't fully understand why this is working, I guess I need to
improve my knowledge of Groovy. If anyone can shed any light on why this
works I'd be grateful. 





--
View this message in context: http://grails.1312388.n4.nabble.com/Grails-2-3-x-Promise-issues-tp4652332p4652336.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email






If you reply to this email, your message will be added to the discussion below:
http://grails.1312388.n4.nabble.com/Grails-2-3-x-Promise-issues-tp4652332p4652354.html
To unsubscribe from Grails 2.3.x Promise issues, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: Grails 2.3.x Promise issues (resolved I think)

gatherer
In reply to this post by Toby Hobson
I think the correct for loop syntax is:

for(String url in urls) {
  ...
}