Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Gambas module file
- ' How many parallel gets we'll allow
- ' (make sure you define all the relevant xxx00_Finished(), xxx01_Finished()... xxx99_Finished() SUBs)
- PRIVATE CONST MAX_SLOTS AS Integer = 10
- ' An array of booleans indicating 'slots' for http clients working in parallel
- ' If it's FALSE it's NOT BUSY, TRUE means this slot is BUSY
- PRIVATE slots AS NEW Boolean[MAX_SLOTS]
- ' An Object array to hold our httpclients
- PRIVATE getters AS NEW Object[MAX_SLOTS]
- ' A test SUB, if you call this you should find slots 0 to 4 get freed up depending on how fast each web site responds to the get (so sometimes it'll be 0,1,2,3,4 or maybe 0,2,1,3,4 or 0,1,2,4,3 etc. etc.)
- PUBLIC SUB getURLs()
- DIM sURL AS String
- DIM rURLs AS String[] = ["http://www.google.com", "http://www.cnn.com", "http://www.slashdot.com", "http://www.gambas.org", "http://www.youtube.com"]
- FOR EACH sURL IN rURLs
- doGet(sURL)
- NEXT
- END
- ' Essentially your code out of the FOR loop...
- PRIVATE SUB doGet(sURL AS String)
- DIM freeSlot AS Integer
- freeSlot = firstFreeSlot()
- PRINT "First free slot:" & freeSlot
- ' Mark the slot busy
- slots[freeSlot] = TRUE
- getters[freeSlot] = NEW HttpClient AS "hPachubeFetch0" & freeSlot
- getters[freeSlot].URL = sUrl
- getters[freeSlot].TimeOut = 8
- 'hPachubeFetch.Tag = sFeed & "|0|12" ' feed | datastream | device id
- 'hPachubeFetch.Auth = 1
- 'hPachubeFetch.User = "user"
- 'hPachubeFetch.Password = "password"
- getters[freeSlot].Async = TRUE
- getters[freeSlot].Get()
- END
- ' Just determine the index of the first free (FALSE) slot
- PRIVATE FUNCTION firstFreeSlot() AS Integer
- DIM idx AS Integer
- DIM slot AS Boolean
- FOR idx = 0 TO slots.Count - 1
- IF slots[idx] = FALSE THEN
- RETURN idx
- END IF
- NEXT
- RETURN -1
- END
- ' Is there a better way to do this??
- ' Simple enough but quite some copy-paste and you need to correct
- ' stuff 10 times... but it works!
- PUBLIC SUB hPachubeFetch00_Finished()
- slots[0] = FALSE
- PRINT "Slot 0 freed up"
- END
- PUBLIC SUB hPachubeFetch01_Finished()
- slots[1] = FALSE
- PRINT "Slot 1 freed up"
- END
- PUBLIC SUB hPachubeFetch02_Finished()
- slots[2] = FALSE
- PRINT "Slot 2 freed up"
- END
- PUBLIC SUB hPachubeFetch03_Finished()
- slots[3] = FALSE
- PRINT "Slot 3 freed up"
- END
- PUBLIC SUB hPachubeFetch04_Finished()
- slots[4] = FALSE
- PRINT "Slot 4 freed up"
- END
- PUBLIC SUB hPachubeFetch05_Finished()
- slots[5] = FALSE
- PRINT "Slot 5 freed up"
- END
- PUBLIC SUB hPachubeFetch06_Finished()
- slots[6] = FALSE
- PRINT "Slot 6 freed up"
- END
- PUBLIC SUB hPachubeFetch07_Finished()
- slots[7] = FALSE
- PRINT "Slot 7 freed up"
- END
- PUBLIC SUB hPachubeFetch08_Finished()
- slots[8] = FALSE
- PRINT "Slot 8 freed up"
- END
- PUBLIC SUB hPachubeFetch09_Finished()
- slots[9] = FALSE
- PRINT "Slot 9 freed up"
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement