Re: [Gambas-user] HttpClient question, now massively parallel!
Brought to you by:
gambas
From: Caveat <Ga...@ca...> - 2011-03-30 17:03:04
|
Thanks Benoit, I knew there had to be a simpler way :-D On Wed, 2011-03-30 at 17:08 +0200, Benoît Minisini wrote: > > Hi Ron_2nd > > > > You're welcome! I made a couple of improvements while I thought of > > them. It's probably better not to add the leading zero to the name of > > the HttpClient, so just do something simple like NEW HttpClient AS > > "tube" & freeSlot, then your event handling routines can be simply > > tube0_Finished(), tube1_Finished()... tube200_Finished() etc. You'll > > probably need to take account of other events too like Connect, Error, > > and Read (I presume in Read you'll actually be getting your data, so > > you'll need to handle a collection of buffers or something...). You'll > > most likely also need to allow for timeouts. > > > > I'd also suggest to make the numbered event handlers simple one-liners > > that call back to a real handler... so for the Finished event: > > > > ' This is the real handler for Finished, it takes a param of the index > > ' of the httpClient that raised the Finished event > > PUBLIC SUB tube_Finished(clientIndex as Integer) > > ' indicate that the slot has come free > > slots[clientIndex] = FALSE > > PRINT "Slot " & clientIndex & " finished" > > PRINT "Processed GET: " & getters[clientIndex].URL > > ... do some real work! > > END > > > > ' Each of the Finished event handlers is now super-simple and > > ' should never need to change, as the real event handler (above) does > > ' the work > > PUBLIC SUB tube0_Finished() > > tube_Finished(0) > > END > > > > PUBLIC SUB tube1_Finished() > > tube_Finished(1) > > END > > > > ... > > PUBLIC SUB tube200_Finished() > > tube_Finished(200) > > END > > > > I'm wondering if there isn't a way to programmatically generate the > > event handlers, and have them know which event fired... freeing you from > > the pain of copy-pasting a bunch of one-line SUBs. > > > > You may want to get real fancy and start queuing requests once all your > > slots are used up, or maybe just deny requests (note I return -1 if > > there's no free slot) until something comes free again... > > > > Have fun, let me know if anything is not clear. > > > > Regards, > > Caveat > > > > P.S. Should I have added a disclaimer that my code is not certified for > > use in real-time radiation monitoring systems upon which the fate of the > > world may depend? ;-) > > > > You don't have to create one event handler for each HttpClient of course! Just > use the same event handler. > > Then how to make the difference between all HttpClient object? > > 1) Use the LAST keyword to find the HttpClient inside the array where it is > stored. > > 2) Or store the index inside the HttpClient.Tag property. That way you get it > directly without having to do a search. > > Regards, > |