nikp
YaBB Newbies
Offline
Posts: 21
vancouver, bc
Async String Loader
Apr 12th , 2010, 10:29am
Hi all, after a 6 months absence i have got back to Processing. I was trying to build a requestStrings() loader using requestImage() as a basis. Typically i am using it to make requests into an API. In this case www.etsy.com database is what the ETSY API is interrogating and i am asking for the typical stuff like user data and relationships between users. I make upwards of 1000-2000 call in maybe 30 secs, mixed in with requestImage() calls also. There are some differences between requestStrings() and requestImage() in that requestStrings() has some error retrying code embedded and also everything around the actual string handling is different of course. I have been using the code with 0184. Couple of caveats...the code does work correctly under considerable real life load, however i have not yet build a unit test. The layout is still a bit untidy and you can see my considerable abuse of println as a development tool ;o) Ideas, thoughts, bugs, errors etc is what i am looking for. And here is the code itself .. volatile int requestsInPlay = 0; public int requestStringsMax = 10; volatile int requestStringsCount = 0; public class RequestStringsHolder{ public String vessel[]; int loaded; //-2 we are in the process of loading, -1 not started to load yet, 0 failed load, 1 loaded public RequestStringsHolder() { loaded = -1; } public int getLoaded() { return(loaded); } } public RequestStringsHolder requestStrings(String filename) { RequestStringsHolder holder = new RequestStringsHolder(); requestsInPlay++; //println("Incoming requestsInPlay "+requestsInPlay+" " +filename+""); return requestStrings(filename, null, holder); } public RequestStringsHolder requestStrings(String filename, String extension, RequestStringsHolder holder) { AsyncStringsLoader ail = new AsyncStringsLoader(filename, extension,holder); ail.start(); return holder; } class AsyncStringsLoader extends Thread { String filename; String extension; RequestStringsHolder holder; int retryLoad; public AsyncStringsLoader(String filename, String extension, RequestStringsHolder holder) { this.filename = filename; this.extension = extension; this.holder = holder; retryLoad = 3; //something better than one but not having tons of exception messages spewing out holder.loaded = -2; //say we are loading } //this Runnable Interface basically makes this chunk of code run on a seperate thread as soon as this object is created public void run() { //run this code while(holder.vessel == null && retryLoad-- >0) { while (requestStringsCount == requestStringsMax) { //println("o "+requestStringsCount+" "+requestStringsMax+"inplay "+requestsInPlay+" " +filename+""); try { Thread.sleep(10); } catch (InterruptedException e) { } } requestStringsCount++; //println("BEFORE AsyncStringsLoader run("+(3-retryLoad)+") " +System.nanoTime()+""); holder.vessel = loadStrings(filename); try { Thread.sleep(10); } catch (InterruptedException e) { } } //println("o "+requestStringsCount+" "+requestStringsMax+"inplay "+requestsInPlay+" " +filename+""); //println("!!!!!!!!!!!!!!AFTER AsyncStringsLoader run() " +System.nanoTime()+""); // An error message should have already printed if (holder.vessel == null) { holder.loaded = 0; requestsInPlay--; //println("@@@@@@@@@@@@@@@ Outgoing WE DIDNT GET THAT requestsInPlay "+requestsInPlay+" " +filename+"VESSEL ==NULL"); } else if (holder.vessel.length == 0) { holder.loaded = 0; requestsInPlay--; //println("@@@@@@@@@@@@@@@ Outgoing WE DIDNT GET THAT requestsInPlay "+requestsInPlay+" " +filename+" VESSEL LENGTH == 0"); }else { holder.loaded = 1; requestsInPlay--; //println("Outgoing requestsInPlay "+requestsInPlay+" " +filename+""); } requestStringsCount--; } }